User Tools

Site Tools


tech:cheatsheets:linux:nixos

NixOS

Install in-place

Infect system using nixos-infect

Ensure you're root when executing the commands (sudo -i)

If any profile is installed for root (re)move the existing profile at /nix/var/nix/profiles/per-user/root before infecting.
touch /etc/ssh/ssh_host_placeholder_key
curl https://raw.githubusercontent.com/elitak/nixos-infect/master/nixos-infect | NIX_CHANNEL=nixos-24.11 NO_REBOOT=1 bash -x

Apply configuration

Check-out your desired configuration into /etc/nixos and adjust it.

git clone <NIXOS-GIT-REPOSITORY> /etc/nixos
/nix/var/nix/profiles/per-user/root/profile/bin/nix-shell \
    -I nixpkgs=$(realpath $HOME/.nix-defexpr/channels/nixos) \
    -p nixos-install-tools \
    --run 'nixos-generate-config --show-hardware-config' \
    > /etc/nixos/hardware-configuration.nix
 
# If your configuration is flake-based
/nix/var/nix/profiles/per-user/root/profile/bin/nix-shell \
    -I nixpkgs=$(realpath $HOME/.nix-defexpr/channels/nixos) \
    -p nixos-rebuild \
    --run 'nixos-rebuild boot --flake /etc/nixos'
 
# For legacy configurations
/nix/var/nix/profiles/per-user/root/profile/bin/nix-shell \
    -I nixpkgs=$(realpath $HOME/.nix-defexpr/channels/nixos) \
    -p nixos-rebuild \
    --run 'nixos-rebuild boot -I nixpkgs=$(realpath $HOME/.nix-defexpr/channels/nixos) -I nixos-config=/etc/nixos/configuration.nix'

Add directories and files you want to keep to /etc/NIXOS_LUSTRATE. These directories and files will not be moved into /old-root upon replacement.

Also ensure that if your primary partition is an encrypted partition to add relevant configuration to your hardware-configuration.nix.

Rescue

Should the system not boot you can use another NixOS system (e.g. from another partition or live CD) to adjust system configuration for example LUKS partitions.

When second NixOS system is booted mount your system into /mnt including additional partitions like boot partition (into their respective directories). Afterwards invoke nixos-enter to enter a shell simulating the future NixOS system.

mount /dev/disk/by-id/xxxx-part1 /mnt
mount /dev/disk/by-id/xxxx-part2 /mnt/boot
nixos-enter

Now adjust your configuration to add/support this system and its hardware. Also keep in mind that if you're using LUKS for your primary partition/disk you need to manually add it to the generated hardware configuration to support it.

# Adjust files in /etc/nixos to support this system
nixos-generate-config > /etc/nixos/hardware-configuration.nix

Now activate the configuration on next boot.

nix-env --set \
    -I nixpkgs=$(realpath $HOME/.nix-defexpr/channels/nixos) \
    -f '<nixpkgs/nixos>' \
    -p /nix/var/nix/profiles/system \
    -A system
/nix/var/nix/profiles/system/bin/switch-to-configuration boot
# this might also work instead of the above commands
# nixos-rebuild boot

Troubleshooting

System is not lustrated

For some reason a system is not lustrated (NOTE: system must boot via GRUB for lustrate to work aparently). If that happens a boot might fail due to systemd not finding default.target. If that happens a manual lustration might help (= move files manually from root into subfolder).

Apparently systemd must also not be enabled in initrd for it work.

tech/cheatsheets/linux/nixos.txt · Last modified: by waldemar

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki