Commands

Updated: November 3, 2024

Common commands used in NixOS | Nix

There are different aspects to commands depending on where they are being used.
They can be in Nix of various operating systems. As well as full blown NixOS.
There is also shell and nix repl. repl is an interactive shell.


Table of Contents



tldr

nix run # runs a packaged binary
\__outputs.packages."SYSTEM".default

nix build # builds a package
\__outputs.packages."SYSTEM".default

nix develop # activates a dev shell
\__outputs.devShells."SYSTEM".default

nixos-rebuild # builds a nixos system
\__outputs.nixosConfigurations."HOSTNAME"

home-manager # builds a home configuration
\__outputs.homeConfigurations."USERNAME"

Help | Info | Basics

# check nixos version
nixos-version

# nixos version plus help
nixos-help

# check nixos configuration section of man page
man 5 configuration.nix

# show nixpkgs flakes
nix registry list

# collect nix unreferenced store paths
nix-collect-garbage

# also collect profile | system generations
nix-garbage-collect -d

# show nix settings
nix show-config

nix eval <channel>.<package>.outPath

# a package in nixpkgs like tailscale
nix eval nixpkgs.tailscale.outPath
nix search nixpkgs tailscale

# a package in a flake
nix eval .#nixos.tailscale.outPath

# a package that is system also needs sudo
sudo nix eval nixos.tailscale.outPath

 # evaluate a nix expression result without building it
nix eval -f . nixos
  --raw     # exclude surrounding quotes and terminate newline

we can also use nix repl for searching nixpkgs

nix repl            # enter nix repl
:l <nixpkgs>        # load nixpkgs else it is only searching pwd

Now that nixpkgs is loaded, we can search for tailscale by starting to type
and hitting tab for autocomeplete results.


nixos-build

# build and activate flake for first time
nixos-build --flake .#nixos

# see generational build history
nixos-build list-generations

Copy

# Can copy build closures as deployment to servers
nix-copy-closure
  --to ssh://host

rebuild on nixos || darwin

# rebuild system && activate
nixos-rebuild switch --flake .#nixos
darwin-rebuild switch --flake .#darwin

# rebuild && update bootloader != activating
nixos-rebuild boot --flake .#nixos

# rebuild system && activate != update bootloader
nixos-rebuild test --flake .#nixos

# see if rebuild works
nixos-rebuild --dry-run

# see if rebuild of flake works
nixos-rebuild --dry-run --flake .#nixos

# rollback to previous state
nixos-rebuild rollback

# build on host A deploy to B, authorized with sudo
nixos-rebuild --build-host A --target-host B --use-remote-sudo

Nix Edit

# edit an attribute in a set (for instance something in nixpkgs)
nix edit nixpkgs#openssl        # this will clone

nix-instantiate

# check version of pkg
nix-instantiate --eval -A <pkg>.version

nix-store

# show store path for a result
readlink result

# find and link identical files
nix-store --optimise

# print all gc roots
nix-store --gc --print-roots

# show all dependencies of a drv
nix-store --query --tree /nix/store/...

# size of a drv
du -cLsh $(nix-store --query --tree /nix/store/...)

# closure is a list of a dependencies in order to use a derivation
nix-store -qR `which man`
nix-store -q --tree `which man`     # nicer view

# foresee software potential, esp if deploying to cloud
nix-copy-closures
nix-store --export

nh

# rebuilt and switch
nh os switch

# rebuild and switch after boot
nh os boot

# rebuild and activate but no switch (no boot entry)
nh os test

# build and activate a home manager configuration
nh home switch --help

-a, --ask
-h, --help
-n, --dry
-s, --specialisation <specialisation>
-u, --update
-v, --verbose

  , --no-nom      # dont use nix-output-manager for the build process

-H, --hostname <hostname>
-S, --no-specialisation

# show what a cleanup would do
nh clean all --keep 5 -n
nh clean all --keepsince 40d2h --keep 10 -n

nom

nom build

nom shell nixpkgs#neovim

nom develop

nvd

# see diff on build generations (here 80 and 81)
nvd diff /nix/var/nix/profiles/system-{80,81}-link