Skip to content

Nix and NixOS

You’ll hate it until you love it. Welcome to the abyss.

https://nlewo.github.io/nixos-manual-sphinx/index.html

Important

We are now using deterministic-nix which has additional, yet unexplored functionality

Useful Snippets#

Remote Building to a Remote Cache using Multiple Architectures#

This is useful for building a system configuration for a system that consumes multiple architectures, such as a Mac Mini with both x86_64 and aarch64 architectures, and a remote cache for the builds. The local machine doesnt cache anything, it just builds the system closure and sends it to the remote cache.

nix build .#system-bedford-kitchen-macmini \
  --builders 'ssh://tsunami@bedford-kitchen-macmini?ssh-key=/home/tsunami/.ssh/id_ed25519 x86_64-darwin; \
  ssh://bcraton@100.68.115.114?ssh-key=/home/tsunami/.ssh/id_ed25519 x86_64-darwin,aarch64-darwin; \
  ssh://tsunami@ereshkigal x86_64-linux,aarch64-linux' \
  --store 'ssh-ng://ereshkigal' \
  &| nom

Copying NixOS Configurations#

Using Nix copy

nix copy --to ssh://user@host:port/path/to/store .#system-octopi
# or
nix copy --to ssh://host ./result

An SD Image for Raspberry Pi#

Note: Needs the moduleClosue overlay and "${inputs.nixpkgs}/nixos/modules/installer/sd-card/sd-image-aarch64.nix" to work

nom build .#nixosConfigurations.octopi.config.system.build.sdImage
sudo dd if=result/sd-image/nixos-sd-image-*-aarch64-linux.img of=/dev/sdh bs=4096 conv=fsync status=progress

Debugging#

Repl#

nix repl is your friend.

  1. enter repl
  2. :lf . - Loads the flake into the context. Needs redone after file changes.
  3. :te - Enables trace outputs (toggles)
  4. :p config.some... - Prints the evaluated value to screen or errors
  5. :.<tab> - Search through what is available to look at

Debugging and Analysis Commands#

For deeper analysis of your configuration:

# Show detailed derivation information
nix show-derivation .#nixosConfigurations.octopi.config.system.build.toplevel

# Get closure information
nix path-info --recursive .#nixosConfigurations.octopi.config.system.build.toplevel

# Analyze what will be built
nix build --dry-run .#nixosConfigurations.octopi.config.system.build.toplevel

Using nix eval for Different Outputs#

For various system components, you can use nix eval:

# Get the toplevel system path
nix eval --raw .#nixosConfigurations.octopi.config.system.build.toplevel

# Get networking information
nix eval --json .#nixosConfigurations.octopi.config.system.networking | jq 

# Get just the derivation store path
nix eval --raw .#nixosConfigurations.octopi.config.system.build.toplevel.drvPath