Run hello from Nix:
nix run nixpkgs#hello
Traditionally nix-channel --list would show which channel this came from; e.g., nixpkgs https://nixos.org/channels/nixos-unstable (or a fixed version) - but channels shouldn't be used any more nowadays anyway.
If that is empty, such as in a modern Nix installation like Determinate's, then its flake registry maps nixpkgs via the extra-nix-path = nixpkgs=flake:nixpkgs in /etc/nix/nix.conf. You can therefore also run: nix run flake:nixpkgs#hello. nix flake metadata nixpkgs will show the Resolved and Locked URLs, e.g., github:NixOS/nixpkgs/nixpkgs-unstable; this comes from nix registry list. With nix run flake:nixpkgs#hello -- --version we can see that the version of hello on nixpkgs-unstable is e.g. 2.12.2.
Running nix run github:NixOS/nixpkgs/24.11#hello -- --version gives us 2.12.1 ... from https://github.com/NixOS/nixpkgs/blob/24.11/pkgs/by-name/he/hello/package.nix.
nix flake new flakes/trivial
created flakes/trivial/flake.nix, which we need to git add ..
Now we can cd flakes/trivial and then nix run (or just nix run ./flakes/trivial, without cd) the default output and... voilà, hello again! (nix run without arguments runs the default package. It's equivalent to nix run .# and nix run .#default.)
If nix run .#hello -- --version shows e.g. hello 2.12.2, we could replace unstable in the flake.nix with e.g. 24.11 to get e.g. a hello 2.12.1 again as above. The big difference is that, this time, it's more "stable" because we've declaratively fixed this version.
BTW: This very first flake.nix comes from the trivial template, because of this.
nix run ./flakes/trivial#hoi
$ hello
fish: hello: command not found...
$ nix shell ./flakes/trivial
$ hello
Hello, world!
Before Flakes, old style, not recommended anymore:
nix-shell -p hello
nix-shell --pure --packages hello
nix flake check ./flakes/trivial --all-systems
TODO Run this in a GitHub Action?
Run nix run ./flakes/java#hello, see java.
Using nix build ./flakes/java#hello (instead of run) produces the installed output that can be run with result/bin/hello.
Note the devShells in java; this allows us to:
cd flakes/java
nix develop
protoc --version
Unfortunately nix develop does not respect your $SHELL (it always lands you bash),
but entering the Dev Shell with the develop.sh script fixes that.
nix flake show templates shows other templates. E.g. nix flake new -t templates#go-hello flakes/go-hello created flakes/go-hello/flake.nix, which we can nix run ./flakes/go-hello again.
TODO FIXME it's broken...
Configuring https://github.com/numtide/treefmt-nix for https://github.com/NixOS/nixfmt, as in the flake.nix, with a treefmt.nix allows running nix flake check (e.g. on CI) to validate formatting, and nix fmt to fix it (e.g. in a pre-commit).
Install https://github.com/oxalica/nil into the devShell, see flake.nix.
Install https://github.com/nix-community/vscode-nix-ide via extensions.json and configure it via settings.json.
For VSC to find nil and nixfmt, you have to start it from the Terminal after entering nix develop.
TODO https://marketplace.visualstudio.com/items?itemName=mkhl.direnv for direnv.
nix search evaluates locally.
https://search.nixos.org/packages is simpler.
nix flake show is useful. Run either in a directory with a flake.nix, or as e.g. nix flake show "git+https://git.sr.ht/~kerstin/sway-timetracker?ref=main" or of course even nix flake -show -all-systems --legacy "github:nixos/nixpkgs?ref=nixos-24.11" - because nixpkgs itself is also really just a (huge) Flake!
https://flakehub.com/flakes is a sort of "Forge" (?) for many more other Flakes; e.g. nix flake show "https://flakehub.com/f/0x5a4/nand2tetris-flake/1.0.0". It mirrors nixpks as https://flakehub.com/flake/NixOS/nixpkgs, so e.g. nix run "https://flakehub.com/f/NixOS/nixpkgs/*#hello" instead of nix run nixpkgs#hello.
PS: The legacyPackages naming here is just due to this (that's why --legacy above; the --all-systems is unrelated and for showing all "platforms").
nix-shell(non-pure) doesn't really give the required isolation, because you may still use local packages - so don't use that.nix-shell --puredoesn't really work in practice, because it reads $HOME dotfiles, but then misses packages on$PATH- don't use.nix developbasically has the same problems - also don't use?!
To "globally" install packages (like a traditional package manager would) imperatively:
$ nix profile install nixpkgs#hello
$ ~/.nix-profile/bin/hello
Hello, world!
You would normally put ~/.nix-profile/bin/ on your PATH.
But don't ever manage profiles imperatively like this, but use Home Manager instead fully declaratively.
PS: nix-env -i hello is the "old" way.
See https://github.com/vorburger/vorburger-dotfiles-bin-etc#nix for https://nix-community.github.io/home-manager.
$ n repl
nix-repl> :help
nix-repl> 1+1
2
nix-repl> greet="world"
nix-repl> "hello, ${greet}"
"hello, world"
nix-repl> double = x: x*2
nix-repl> let double = x: x*2; in double 3
nix-repl> double 3
6
nix-repl> builtins.trace "hey" true
nix-repl> import ./lang/first-test.nix { arg1 = "default"; }
"same!"
nix-repl> :l <nixpkgs>
Added 15102 variables.
nix-repl> lib.unique [ 1 2 3 1 ]
[ 1 2 3 ]
https://nixos.org/guides/nix-pills/our-first-derivation.html then explains the real power.
nix eval --raw --impure --expr 'builtins.currentSystem'
Containers from Nixery
podman run --rm -ti nixery.dev/shell/git/htop bash
podman run --rm -t nixery.dev/hello hello
podman unshare
podman image mount nixery.dev/hello
ls -al $(podman image mount nixery.dev/hello)
podman image umount nixery.dev/hello
nix.conf~/.nix-profilebin/is on$PATHetc/,lib/,shareare like/etc,/lib,/share
~/.nix-channelsTODO, after reading more about channels~/.nix-defexprTODO
It's pretty cool how (at least on Fedora) e.g. man direnv still works after nix profile install nixpkgs#direnv even though it's not a DNF system package.
My docs!
See §Learning Bookmarks.