Skip to content

Nix

  • Building Nix packages nix-build involves two steps:
    • evaluation/instantiation (nix-instantiate): goes from nix expression (.nix) to a derivation (.drv)
    • realization (nix-store --realize): goes from a derivation to a store path
  • build-loop is how Nix builds software
    flowchart LR
        A[Substitutors] --> B{remainingOutput?}
        B --No--> C[BuildInputs]
        C --> D[Build]
        D --> E[RegisterOutputPaths]
        B --Yes--> Stop
  • Nix package is unique across: source, dependencies, build steps, architecture, platform
    • Nix can determine the derivation and the final output path ahead of time (needed for caching)
  • derivation is platform-specific way to build a package
    • output of derivation depends only on the inputs and the build script, and each package has a unique hash
  • NixOS boot process:
    1. Bootloader loads kernel and initrd
    2. initrd mounts partitions
    3. initrd runs /run/current-system/activate
    4. if first time, it populates POSIX directories (/bin, /etc, /run, /usr, /var)
  • Nix binary cache
    • NAR file is a serialized file format for packages used by the Nix package manager (v/s tar files) because they don't contain any non-deterministic information
    • .narinfo files contain metadata info about each package, such as dependencies
  • Use of mkIf within module system requires all options to be valid

Useful commands

Flakes

  • Allow unfree
    let pkgs = import nixpkgs { system = "x86_64-linux"; config.allowUnfree = true; };
    
  • Make nixpkgs refer to the same one in your flake enabled nixos
    nix.registry.nixpkgs.flake = inputs.nixpkgs;