KEMBAR78
Cheatsheet | PDF | Computer Programming | Software Engineering
0% found this document useful (0 votes)
46 views2 pages

Cheatsheet

The document provides a comprehensive cheatsheet for managing Nix and NixOS systems, including commands for rebuilding the system, managing packages, and utilizing flakes. It also includes references to important documentation links and examples of Nix language syntax and functions. Additionally, it covers garbage collection and project-specific shells for package management.

Uploaded by

Subash Karki
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views2 pages

Cheatsheet

The document provides a comprehensive cheatsheet for managing Nix and NixOS systems, including commands for rebuilding the system, managing packages, and utilizing flakes. It also includes references to important documentation links and examples of Nix language syntax and functions. Additionally, it covers garbage collection and project-specific shells for package management.

Uploaded by

Subash Karki
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Input Management NixOS System Rebuild

Nix / NixOS Cheatsheet Flakes Rebuild system and acti-


vate
nixos-rebuild switch
Init flake project nix flake init

Init flake-parts nix flake init -t github:hercules-ci/ Rebuild system and activa-
flake-parts te for now without upda- nixos-rebuild test
Most Important Documentation Links project
ting bootloader
Update flake in-
nix flake update
Nix Docs (Tool, Language) https://nixos.org/manual/nix puts
Rebuild w/o activating, but
nixos-rebuild boot
Update and com- update bootloader
nix flake update --commit-lock-file
Nixpkgs (Packaging, prog https://nixos.org/manual/ mit lock file
langs, library) nixos-rebuild switch
nixpkgs Update specific Rollback
nix flake lock --update-input <name> --rollback
input
NixOS https://nixos.org/manual/nixos nixos-rebuild switch
Niv (Pre Flakes) Build on host a, deploy to --build-host a
Nixpkgs package and NixOS Install Niv Files niv init host b, authorize with sudo --target-host b
module search engine
https://search.nixos.org --use-remote-sudo
Add GitHub Re-
niv add <github-owner>/<reponame>
pository
Imperative Package Management Note: nixos-rebuild expects a flake /etc/nixos/flake.nix to
Update inputs niv update exist, and within that flake, a nixosConfiguration attribute
Update package list apt update happens automatically with the hostname of the current system.
Update specific
niv update <name>
input
apt search nix search nixpkgs
Search
<pkgname> <pkgname> Switch branch/
niv update <name> -b <gitref>
Garbage Collection
tag of input
apt install nix profile install Collect unreferenced nix
Install nix-collect-garbage
<pkgname> nixpkgs#<pkgname> store paths
nix profile upgrade
Flake References
Upgrade installed apt upgrade Also collect old profile/
'.*' Flake in current Directory . nix-collect-garbage -d
system generations
List installed dpkg -l nix profile list Local path [path:]/path/to/repo
nix-collect-garbage --max-
HTTPS URL to flake tarball https://host/flake.tar.gz Only delete up to 50GB
apt remove nix profile remove freed 50G
Remove
<pkgname> <list-number> Git Repo via HTTPS git+https://host/repo
- nix profile rollback Find and link identical files nix-store --optimise
Rollback Git Repo via SSH git+ssh://git@host/repo

GitHub Repo github:owner/repo Print all GC roots nix-store --gc --print-roots


Per-Project Shells git+ssh://git@host/re-
nix shell nixpkgs#pkg1 Specific Branch/Tag
po?ref=abc123
or
Ad-hoc shell with packages
nix shell
nixpkgs#{pkg1,pkg2} Nix REPL
Project-shell with flake nix develop Start Nix REPL nix repl

Project-shell with shell.nix or Load local Flake :lf .


nix-shell
default.nix file Build derivation :b attribute.with.derivation
Build & Install derivation :i attribute.with.derivation
Building Packages Fully print expression :p some.expression
Build default.nix or default pkg Show documentation of
nix build :doc builtins.listToAttrs
from flake builtin function
Professional Nix & NixOS Trainings
Build specific attributes (flakes) nix build .#pkg1 .#pkg2 Show REPL help :?
https://nixcademy.com/
let
let f = set@{ x, … }: x + set.y;
Full attribute set match
Nix Language Cheat Sheet Reference attribute keys in
s = { x = { y = 1; }; };
(potential anti-pattern)
in
f { x = 1; y = 2; }
s.x.y # ⇒ returns 1 # ⇒ returns 3

let let
Most Important Documentation Links Reference optional attribute set = { x = 1; }; f = x: if x == 0
Nix builtins.* func- https://nixos.org/manual/nix/stable/ keys in then 0
Recursive function calls itself
tions language/builtins set.y or 2 # ⇒ returns 2 else x + f (x -1);
in
Nixpkgs function library https://nixos.org/manual/nixpkgs Note: Variable assignments exist only in 3 places: attribute f 3 # ⇒ returns 6
sets, let-in blocks, nix REPL
Nixpkgs function search
https://noogle.dev
engine [ 1 2 ] ++ [ 3 4 ] Other Special Syntax
List concatenation
# ⇒ returns [ 1 2 3 4 ]
let
Types let x = 1; in { inherit x; } x = "bar";
String "this is a string" Inherit same as String interpolation in
let x = 1; in { x = x; } "foo ${x} baz"
'' # ⇒ returns "foo bar baz"
Multi-line String foo
bar let x = { y = 1; };
(double single-quotes) "this is \${masked}"
'' in { inherit (x) y; } Masking ${...}
Inherit from scope same as # ⇒ returns "this is ${masked}"
Boolean true, false let x = { y = 1; };
in { y = x.y; } Masking ${...} in
Null null
double single-quote ''this is ''${masked}''
Integer 123, -123 strings
let
Float 3.14 set = { x = 1; y = 2; }; Paths (Copied to nix /an/absolute/path
With-expressions
in store when referen- ./a/relative/path
/an/absolute/path (potential anti-pattern) with set; ced) ./../dir/up/and/down
Path ./a/relative/path
x + y # ⇒ returns 3
./../dir/up/and/down
let
Simple attribute set { a = 1; b = 2; } Key from variable in x = "key";
Nested attribute set { a = 1; b = { c = 3; d = 4; }; } Functions attribute set in
{ ${x} = "value"; }
Recursive attribute set Simple function as in Python: let
rec { x = 1; y = x + 1; }
(potential anti-pattern) f = x: x + 1;
def f(x): in Special Builtin Functions
List [ 3 2.0 "one" null ] f 1 # ⇒ returns 2
return x + 1 Import file and return import ./some/file.nix
expression
Syntax Function with 2 parameters
let assert 1 + 1 == 2; 10
as in Python: Assertion
# a single-line comment f = x: y: x + y; # ⇒ returns 10 without error
Comment in
/* a multi-line comment */ def f(x, y): f 1 2 # ⇒ returns 3 Abort evaluation with abort "this describes the error"
If-then-else if x > 3 then 10 else -10 return x + y error
let let Throw an exception throw "this describes the exception"
x = 1; f = { x, y }: x + y; when referenced
Function with named para-
Local variables y = 2; in
in meters f { x = 1; y = 2; }
x + y # ⇒ returns 3 # ⇒ returns 3

{ x = 1; } // { y = 2; } let
f = { x, y, … }: x + y;
Attribute set # ⇒ returns { x = 1; y = 2;} Match specific parameters,
in
update operator // { x = 1; } // { x = 2; } ignore others f { x=1; y=2; z=10; }
# ⇒ returns { x = 2; }
# ⇒ returns 3

let let
Attribute set set = { x = 1; }; f = { x, y ? 2 }: x + y;
in Function with default values
has-operator in Professional Nix & NixOS Trainings
set ? x # ⇒ returns true f { x=1; } # ⇒ returns 3
https://nixcademy.com/

You might also like