All those who seem stupid, they are, and also so are half of those who do not, Quevedo
NixOS is a unique, innovative, and powerful Linux distribution that leverages the Nix package manager. Unlike traditional Linux distributions that update packages and system configurations in-place, NixOS uses a purely functional and declarative approach to define the system state, reducing the risk of system breakage.
NixOS is a Linux distribution that uses the Nix package manager to handle packages and system configuration in a purely functional manner. This approach ensures that builds are reproducible and that the system state can be reliably replicated or rolled back.
sudo nix-env ‐‐rollback is a command used to revert your system to the previous configuration. It’s a powerful tool for undoing unintended changes or recovering from failed package installations. Before rolling back, consider if there’s a more targeted solution, like uninstalling specific packages or reverting configuration files.
Espanso is a privacy-first, cross-platform Text Expander. It allows you to define custom shortcuts (triggers, e.g., :name, :address, :telephone, etc.) that, when typed, Espanso automatically expand them into longer text snippets, increasing drastically your productivity.
Install its package and rebuild the system. To install Espanso, you need to add it to your system packages. This is typically done in a NixOS configuration file.
environment.systemPackages = with pkgs; [
···
espanso # A cross-platform text expander written in Rust, useful for inserting predefined text snippets.
];
After adding espanso, rebuild your system: sudo nixos-rebuild switch
Manage Espanso’s YAML Configuration using Home Manager. You can manage your Espanso configuration file using Home Manager. home.file allows you to specify where your Espanso configuration is located.
home.packages = [ pkgs.cowsay pkgs.fastfetch ];
home.stateVersion = "24.05";
···
home.file.".config/espanso/match/base.yml".source = ~/dotfiles/espanso/base.yml;
# The home.file option is used to create a file at the specified path (.config/espanso/match/base.yml)...
# by copying it from the given source file (~/dotfiles/espanso/base.yml)
# base.yml is where you define your Espanso matches (triggers and expansions).
After installing and configuring Espanso, you need to register the Espanso service to enable it using the following command: espanso service register
. If you make any changes to your configuration, you can restart Espanso: espanso restart
It sets up Espanso as a background service on your system. It will run hidden in the background, actively monitoring your typing for triggers and expanding them as defined in base.yml.
VirtualBox is a powerful, open-source virtualization software developed by Oracle. It allows you to run multiple operating systems simultaneously on a single physical machine, making it an excellent tool for developers, testers, novice users, and anyone in general who needs to work with different operating systems in the same machine.
These settings are necessary if you're running NixOS inside a VirtualBox virtual machine. They enable VirtualBox Guest Additions, which improve integration between the host and guest systems. Guest Additions provide enhanced functionality, such as better video support, shared clipboard and folders, and improved mouse integration.
virtualisation.virtualbox.guest.enable = true; # Enables VirtualBox Guest Additions
virtualisation.virtualbox.guest.seamless = true;
# Enables seamless mode integration.
# It allows windows from the guest and host to be displayed together, providing a more integrated experience and ...
# making it appear as if applications from the guest are running directly on the host.
virtualisation.virtualbox.guest.clipboard = true;
# Enables clipboard sharing (copy and paste) between host and guest
virtualisation.virtualbox.guest.x11 = true;
# Ensures X11-based graphical improvements, improving display resolution, mouse pointer integration, and overall usability inside the virtual machine.
These settings ensures you have a fully functional audio setup in NixOS using PulseAudio, a popular sound server for Linux.
Add the following lines to your /etc/nixos/configuration.nix:
sound.enable = true; # Enables basic sound support in NixOS (ALSA).
hardware.pulseaudio.enable = true; # Enables the PulseAudio sound server.
# It provides advanced sound management features, such as per-application volume control and network audio streaming.
hardware.pulseaudio.support32Bit = true; # Allows support for 32-bit applications to use PulseAudio.
Ensure your user is part of the audio group to have the necessary permissions for audio devices.
users.users.nmaximo7 = {
isNormalUser = true;
# ...other user settings...
extraGroups = [ "audio" ]; # Add the user to 'audio' group
};
Over time, as you upgrade and rebuild your NixOS system, old package versions and system generations can accumulate. To free up disk space, you can perform garbage collection, which removes theseold and unused packages and system generations from the Nix store: nix-collect-garbage -d
Automatic updates allow NixOS to periodically keep your system up-to-date with the latest package versions, ensuring that you receive security fixes, bug patches, and new features without manual intervention.
These settings automate system maintenance tasks such as garbage collection to free up disk space and automatic system updates to keep your system up to date.
nix = {
# A recurring problem with NixOS is the lack of space on /, it is relatively easy for /nix/store to grow beyond reasonable sizes.
settings.auto-optimise-store = true; # It periodically optimizes the Nix store to save space.
gc = { # Enables automatic garbage collection.
automatic = true; # Automatically cleans up old or unused packages and system generations weekly.
dates = "weekly"; # Schedules garbage collection weekly.
options = "--delete-older-than 7d"; # Deletes generations older than 7 days, ensuring the store doesn't accumulate outdated packages.
};
};
# System Auto-Upgrades
system.autoUpgrade = {
enable = true; # Activates the automatic upgrade feature.
dates = "02:00"; # Schedules the upgrade to run daily at 02:00 AM.
randomizedDelaySec = "45min"; # Adds a random delay (up to 45 minutes) to avoid simultaneous updates across multiple systems (system administrator.)
};
Always remember, after changing your NixOS configuration file(s), run a rebuild of your NixOS: sudo nixos-rebuild switch
While automatic updates provide the convenience of keeping your system and applications up-to-date with minimal effort, manual updates give you greater control over when and how updates are applied. This can be critical for maintaining system stability, avoiding problems during critical tasks, and ensuring that new updates are compatible with your current setup.
sudo nix-channel --update
# This fetches the latest package lists from subscribed channels (it synchronizes local channel metadata with remote sources).
sudo nixos-rebuild switch --upgrade
# Fetches updated versions, rebuilds, and activates the system configuration with the latest packages, making it a convenient way to keep your system up-to-date.
#!/bin/sh
# Ultimate update
# _ _ __ _ _ __ ___ _ _ ___ ___ __ ____ ___
#( \( )( )( \/ )/ \ / __) ( )( )( ,\( \ ( )(_ _)( _)
# ) ( )( ) (( () )\__ \ )()( ) _/ ) ) )/__\ )( ) _)
#(_)\_)(__)(_/\_)\__/ (___/ \__/ (_) (___/(_)(_)(__) (___)
# Setting Variables
cowsay "Update"
echo "Update"
USER=nmaximo7
BACKUP_DEST="/run/media/${USER}/mydisk2"
# This fetches the latest package lists from subscribed channels.
if ! nix-channel --update; then
echo "Failed to update Nix channels."
exit 1
fi
# Rebuild the system
if ! sudo nixos-rebuild switch; then
echo "Failed to rebuild the system."
exit 1
fi
# Update your installed Flatpak applications so you can benefit from the latest features and security updates.
if ! flatpak update; then
echo "Failed to update Flatpak applications."
fi
# Upgrade all containers in Distrobox
if ! distrobox-upgrade --all; then
echo "Failed to upgrade Distrobox containers."
fi
# Upgrade the system. Fetches the latest packages, rebuilds, and activates the new generation, making it a convenient way to keep your system up-to-date.
if ! sudo nixos-rebuild switch --upgrade; then
echo "Failed to upgrade the system configuration."
exit 1
fi
# Version Control: Keep your configuration files under version control to track changes and facilitate rollbacks.
cd /home/nmaximo7/dotfiles
# git status --porcelain: This command returns a list of files that have changes compared to the last commit.
# if [ -n "$(git status --porcelain)" ]... checks if git status --porcelain produces any output.
# If it does, this means there are changes in the working directory or the staging area that haven't been committed yet.
# Then, it commits them (if any are found), ensuring version-controlled configuration changes are always up-to-date.
if [ -n "$(git status --porcelain)" ]; then
git add .
git commit -m "Update NixOS configuration"
git push
else
echo "No changes to commit."
fi
# Monitor disk use
# nix-store --gc --print-dead
# df -h /nix/store
Nix flakes are an experimental feature. They enforce a uniform structure for Nix projects, pin versions of dependencies in a lock file, and make it more convenient to write reproducible Nix expressions.
They are designed to improve reproducibility (it aims to make builds more reproducible by using a flake.lock file to record exact versions of all dependencies), composability (it’s easy to pull in packages and modules from other repositories as flake inputs), and usability of Nix expressions.
1 Step. Modify NixOS Configuration. sudo vim /etc/nixos/configuration.nix
nix = {
package = pkgs.nixFlakes; # Use the Nix package manager with Flakes support.
extraOptions = ''
experimental-features = nix-command flakes
''; # Nix Flakes are an experimental feature, so you must explicitly enable them.
};
2 Step. Rebuild the system to apply changes: sudo nixos-rebuild switch
3 Step. Create a Flake for Your System Configuration by initializing a directory with nix flake init
mkdir flake # Create a Directory for Your Flake
cd flake # Go into this directory.
nix flake init -t templates#nixos-recommended # Initialize a new flake.
# Create a basic flake in the current directory from a template. # It will create a flake.nix using a template.
# The -t templates#nixos-recommended flag specifies a template that provides a recommended flake structure.
vim flake.nix
4 Step. Edit the flake.nix file to define system configurations and integrate Home Manager.
# vim flake.nix
{
description = "NixOS Configuration"; # A brief description of the flake
# The inputs attribute specifies external dependencies for the flake.
# nixpkgs and home-manager are pulled from GitHub
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-24.05"; # Specifies the source of Nix packages from the stable branch of Nixpkgs
home-manager = {
url = "github:nix-community/home-manager/release-24.05";
inputs.nixpkgs.follows = "nixpkgs";
};
};
# The outputs function defines what this flake produces or provides.
# It takes self (the flake itself), nixpkgs (the imported package set), and home-manager as arguments.
outputs = { self, nixpkgs, home-manager, ... }:
let
system = "x86_64-linux"; # Use the defined system architecture
pkgs = import nixpkgs { inherit system; }; # Import the Nixpkgs package
lib = nixpkgs.lib; # Import the Nixpkgs library for utility functions
in {
# Define NixOS system configurations here keyyed by hostname (e.g., your-user) [*]
nixosConfigurations = {
your-user = lib.nixosSystem { # Create a NixOS system configuration named "your-user"
inherit system; # It specifies the architecture for which the NixOS configuration is built. This is usually x86_64-linux for standard desktops and servers.
# lib.nixosSystem builds a full NixOS system configuration from a list of modules.
modules = [ # A list of NixOS module files to include in the configuration.
./configuration.nix # Include the main configuration file for system settings
home-manager.nixosModules.home-manager # It also integrates Home Manager as a NixOS module.
{
home-manager.useGlobalPkgs = true;
home-manager.useUserPackages = true;
# Inside the modules sets up a Home Manager configuration for user "your-user"
home-manager.users.your-user = {
imports = [ ./home.nix];
};
}
];
};
}; # end of nixosConfigurations
};
}
5 Step. Create a Home Manager file (home.nix) for user-level Home Manager settings:
# vim home.nix
# This is your home-manager configuration file
# Use this to configure your home environment
{ config, pkgs, ... }:
# home.username and home.homeDirectory are derived from the NixOS user account.
# Add packages for your user as you see fit.
# These are user-level packages managed by Home Manager.
home.packages = with pkgs; [ htop ];
# Enable git
programs.git.enable = true;
# Nicely reload system units when changing configs
systemd.user.startServices = [ "sd-switch" ];
home.stateVersion = "24.05"; # It must match both your NixOS and Home Manager release version to ensure compatibility.
}
6 Step. Applying Your Flake-Based Configuration
Once your flake.nix and home.nix are set up, you can build and apply your NixOS configuration directly from the flake:
sudo nixos-rebuild switch --flake .#your-user
# --flake .#your-user specifies the flake in the current directory (.) and..
# the your-user configuration defined in flake.nix [*].
sudo nixos-rebuild switch --flake '.#your-user'
# In zsh, "#" is used for comments, so you need to quote the argument.