Functional package manager. Supports easy rollbacks. Language for interacting with nix
package manager. NixOS - OS based on nix. Can facilitate deployments on remote
machines with nixops. Here are some scattered notes about the topic.
nix-shell
Create temporary environments
nix-shell -p prog1 prog2
nix-shell 'expr'
{ pkgs ? import <nixpkgs> {} }:
pkgs.mkShell {
buildInputs = [ pkgs.hello ];
}
# Or
{ pkgs ? import <nixpkgs> {} }:
pkgs.mkShell with pkgs; {
buildInputs = [ hello ];
}
Composes pretty well with direnv. Or even better - use lorri instead of nix-shell.
Pinned nix shell
We would like a easy-to-pin shell.nix with overriding packages. Here it is (Nix 2.0 +).
shell.nix:
let
java = "openjdk8_headless";
pkgs = import ./nix/pkgs.nix { inherit java; };
in pkgs.mkShell {
buildInputs = [ pkgs.${java} pkgs.sbt ];
shellHook = ''
export JAVA_HOME="${pkgs.${java}}"
'';
}
nix/pkgs.nix:
{ java }:
let
pinnedPkgs = (fetchTarball
"https://github.com/NixOS/nixpkgs/archive/d3f7e969b9860fb80750147aeb56dab1c730e756.tar.gz");
config = {
packageOverrides = p: {
jdk = p.${java};
jre = p.${java};
};
};
in import pinnedPkgs { inherit config; }
More info about pinning nixpkgs here.
nix-store
# find related packages to nix path (man nix-store for more)
nix-store --query (--referrers[-closure]|--references) <nix-path>
# delete nix path
nix-store --delete [--ignore-liveness] /nix/store/<...>
# --ignore-liveness - skip checking if package is in use.
People advise using sudo to force deletion. Keep in mind that it could change
owner of /nix/store to root. If installed nix in single-user mode - you have
to bring it back (and probably don’t need sudo in first place 😏).
nix-du
With nix-du we can (somewhat) visualize dependency tree.
nix-du -s=10MB | tred | dot -Tsvg > ~/store.svg
Diff home-manager generations
# requires awk, diff, delta (by default)
generation() {
home-manager generations | awk -v id="$1" '$5 == id {print $NF}'
}
after=204
before=203
after_nix="$(generation $after)"
before_nix="$(generation $before)"
nix-store -qR $after_nix > /tmp/after_nix
nix-store -qR $before_nix > /tmp/before_nix
diff -u /tmp/before_nix /tmp/after_nix | delta
Recipes
Nix check packages version / hash
nix-instantiate --eval -E '(import <nixpkgs> {}).lib.version'
Build package
nix-build -E 'with import <nixpkgs> { }; callPackage ./zap/default.nix { }'
Use self-writen package
home.packages = [
callPackage ./zap/default.nix { }
];
Search packages
nix-env -qaPA nixpkgs.nodePackages #search in nodePackages
nix search <package>
Cleanup nix-store
To cleanup old generations we should(probably 😼)
- remove old home-manager generations:
home-manager expire-generations "-2 days" - run
nix-collect-garbage [-d | --delete-older-than "30d"]-ddeletes all non-current generations (not allowing rollbacks)
Links
- basics of language - nix pills
- nix flakes explaination - DoomEmacs discord
- LUKS on NixOS
- nix-du - visualise dependencies and sizes
- nix on hetzner cloud
- Burke Libbey’s youtube channel
- Nix python workflow
- overcome unavailable binary cache
- Option search
NixOS
Fix command-not-found failing with sqlite errors
https://discourse.nixos.org/t/command-not-found-unable-to-open-database/3807/5
Comments