Nix Reproducible packages

Version 2.35.2 · source

Start here

nix run nixpkgs#hello
Run a package once
nix shell nixpkgs#jq nixpkgs#curl
Shell with tools on PATH
nix search nixpkgs <term>
Find a package
nix develop
Enter the project dev shell
nix build
Build, result symlink
nix flake init
Create a flake.nix
nix flake update
Bump the lock file
nix store gc
Free unused store paths

Concepts

  1. flake.nix inputs and outputs
  2. flake.lock pinned revisions
  3. Derivation .drv build recipe
  4. /nix/store hash-addressed path

Run and try

nix run <installable> [-- <args>]
Run an app from a flake
Arguments after -- go to the program.
nix shell <installable>...
Shell with packages on PATH
nix shell <installable> -c <cmd>
Run one command with the package
nix run nixpkgs#<pkg>
Run a nixpkgs package
nix run .
Run this flake's default app
nix run github:<owner>/<repo>
Run a flake from GitHub
nix run nixpkgs/<branch>#<pkg>
Use a specific nixpkgs branch
nix shell --ignore-environment <installable>
Shell with only these tools
nix bundle <installable>
Pack an app to run without Nix

Build

nix build
Build default package of ./flake
Creates a ./result symlink to the output.
nix build <installable>
Build a given installable
nix build .#<name>
Build a named package of this flake
nix build --no-link --print-out-paths
Build and print store path only
nix build -L
Show full build logs
nix build --rebuild <installable>
Rebuild to check determinism
nix log <installable>
Show a build log
nix path-info -rsSh <installable>
Closure with sizes
nix why-depends <a> <b>
Why a depends on b

Develop

nix develop
Shell with the dev environment
Uses devShells.<system>.default, then packages.<system>.default.
nix develop .#<name>
Use a named devShell
nix develop -c <cmd>
Run a command in that environment
nix develop nixpkgs#<pkg>
Build environment of a package
nix develop --impure
Allow env vars and mutable paths
nix develop --build
Run a single build phase
nix print-dev-env
Print the environment as shell code
nix fmt
Format with the flake's formatter
direnv: use flake
Load the dev shell via nix-direnv
Put it in .envrc; needs direnv and nix-direnv.

Flakes

nix flake init
Flake from the default template
nix flake init -t <template>
Flake from a given template
nix flake new <dir>
New flake in a directory
nix flake show [<flakeref>]
List a flake's outputs
nix flake check
Evaluate and run flake checks
nix flake update
Update all inputs in the lock
nix flake update <input>
Update a single input
nix flake lock
Create missing lock entries
nix flake metadata
Show inputs and revisions
nix flake lock --override-input <in> <ref>
Override an input temporarily
nix flake prefetch <flakeref>
Fetch a flake into the store
nix flake clone <flakeref> --dest <dir>
Clone a flake's repo
Flake references
Form Example
Registry nixpkgs, nixpkgs/nixos-unstable
GitHub github:NixOS/nixpkgs/<ref>
Git git+https://<host>/<repo>?ref=<br>
Path ./dir, path:/abs/dir
Tarball https://<host>/<f>.tar.gz

Profile

nix profile add <installable>
Install into your profile
Called nix profile install in older releases.
nix profile list
List installed packages
nix profile remove <name>
Uninstall a package
nix profile upgrade --all
Upgrade installed packages
nix profile rollback
Back to the previous generation
nix profile history
List profile generations
nix profile diff-closures
Changes between generations
nix profile wipe-history
Delete old generations

Search and inspect

nix search nixpkgs <regex>
Search packages by name or description
nix search nixpkgs <regex> --json
Search results as JSON
nix eval <installable>
Evaluate and print an attribute
nix eval --expr '<expr>'
Evaluate a Nix expression
nix eval --raw <installable>.<attr>
Print a string without quotes
nix edit <installable>
Open a package's source in $EDITOR
nix repl
Interactive evaluator
nix repl nixpkgs
Repl with nixpkgs loaded
:l <flakeref>
Load a flake or file in the repl
Also :r reloads, :q quits, :t shows a type.
nix registry list
Show flake registry aliases
nix registry pin <name>
Pin an alias to its revision

Store and cleanup

nix store gc
Delete unreachable store paths
nix store gc --max <bytes>
Stop after freeing this much
nix store delete <path>
Delete a given path
nix store optimise
Hard-link identical files
nix store verify --all
Check store integrity
nix store repair <path>
Rebuild a damaged path
nix store ls <path>
List a store path's files
nix store diff-closures <a> <b>
Compare two closures
nix copy --to <store> <installable>
Copy a closure to another store
nix hash path <path>
Hash a directory or file
nix config show
Show effective settings
--option <name> <value>
Override one setting for a command
--offline
Do not use substituters
--refresh
Re-fetch cached tarballs and refs
nix upgrade-nix
Upgrade Nix itself
Does not work on multi-user installs managed by a distro or NixOS.

Recipes

Minimal dev shell (flake.nix)
{  inputs.nixpkgs.url =    "github:NixOS/nixpkgs";  outputs = { nixpkgs, ... }: {    devShells.x86_64-linux.default =      nixpkgs.legacyPackages        .x86_64-linux.mkShell {          packages = [ ];        };  };}
Try a tool without installing
nix search nixpkgs ripgrepnix shell nixpkgs#ripgrep -c rg --versionnix run nixpkgs#cowsay -- hi
Pin and update one input
nix flake metadatanix flake update nixpkgsgit diff flake.locknix flake check
Reclaim disk space
nix profile wipe-historynix store gcnix store optimise