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.
- enter repl
:lf .- Loads the flake into the context. Needs redone after file changes.:te- Enables trace outputs (toggles):p config.some...- Prints the evaluated value to screen or errors:.<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
Pipe Operator#
# before
let digital_ocean_loadbalancer_regions = lib.lists.unique (lib.map (value: value.region) (builtins.attrValues (lib.filterAttrs (name: value: value.loadbalancer.enable) digital_ocean_clouds)));
# after
let digital_ocean_loadbalancer_regions = digital_ocean_clouds |> lib.filterAttrs (name: value: value.loadbalancer.enable) |> lib.mapAttrs (name: value: value.region) |> builtins.attrValues |> lib.lists.unique
# before
let
# I hate this but I can't think of a better way to do this
list_of_clouds = lib.map
({name, value}: {name = name; replicas = value.replicas; region = value.region; talos_image_version = value.talos_image_version;})
(lib.attrsToList digital_ocean_clouds);
f = {name, replicas, region, talos_image_version}:
lib.map (replica: {name = name; replica = replica; region = region; talos_image_version = talos_image_version;})
replicas;
x = lib.lists.concatMap f list_of_clouds;
in lib.map createResource x;
# after
digital_ocean_clouds
|> lib.attrsToList
|> lib.map ({name, value}:
{ inherit name; inherit (value) replicas region talos_image_version; }
)
|> lib.map (x:
x.replicas
|> lib.map (replica: { inherit (x) name region talos_image_version; inherit replica; })
)
|> lib.concatLists
|> lib.map createDroplet;