:PROPERTIES:
:ID:       6873de22-9eac-492c-93a8-6cdf8cbfc0f8
:END:
#+title: Nix flakes
#+date: 2021-07-18 22:34:11 +08:00
#+date_modified: 2021-09-14 18:39:43 +08:00
#+language: en


#+begin_note
As of 2021-06-30, the version used for this note is at v2.3 so it needs to be invoked with the unstable version.
#+end_note

- similar to [[https://guix.gnu.org/manual/en/html_node/Channels.html][Guix channels]]
- a collection of packages and functions while making it easy to configure Nix declaratively
- replaces the traditional Nix channels since fully reproducing an environment with Nix requires special care in practice;
  it isn't fully reproducible in practice such as the inclusion of Nix paths embedded in environment variables and access to arbitrary files;
  plus, there's no standard way of composing projects with Nix
- as of 2021-06-30, this is on the unstable version of the Nix package manager and needs some additional configuration

why flakes?

- provides a structure for discoverability
- makes 100% reproducibility a little easier with Nix
- in case you're using NixOS, it also provides an easier way to extend it with third-party custom modules

Here's an example to interact with a flake.
It will show the entire outputs of a flake as well as the normalized version of the flake object.

#+name: flake-sample-object
#+begin_src python  :results value silent :exports none
return "github:edolstra/dwarffs"
#+end_src

#+begin_src shell  :shebang "#!/usr/bin/env nix-shell"
#! nix-shell -i bash -p nixUnstable
nix --experimental-features 'nix-command flakes' flake show <<flake-sample-object()>> | sed -e "s/\x1b\[.\{1,5\}m//g"
#+end_src

#+results:
#+begin_example
github:edolstra/dwarffs/f691e2c991e75edb22836f1dbe632c40324215c5
├───checks
│   ├───aarch64-linux
│   │   ├───build: derivation 'dwarffs-0.1.20210121.f691e2c'
│   │   └───test: derivation 'vm-test-run-unnamed'
│   ├───i686-linux
│   │   ├───build: derivation 'dwarffs-0.1.20210121.f691e2c'
│   │   └───test: derivation 'vm-test-run-unnamed'
│   └───x86_64-linux
│       ├───build: derivation 'dwarffs-0.1.20210121.f691e2c'
│       └───test: derivation 'vm-test-run-unnamed'
├───defaultPackage
│   ├───aarch64-linux: package 'dwarffs-0.1.20210121.f691e2c'
│   ├───i686-linux: package 'dwarffs-0.1.20210121.f691e2c'
│   └───x86_64-linux: package 'dwarffs-0.1.20210121.f691e2c'
├───nixosModules
│   └───dwarffs: NixOS module
└───overlay: Nixpkgs overlay
#+end_example

Let's build from one of the outputs of call_flake-sample-object().

#+begin_src shell  :shebang "#!/usr/bin/env nix-shell"
#! nix-shell -i bash -p nixUnstable
nix --experimental-features 'nix-command flakes' build 'github:edolstra/dwarffs#checks.aarch64-linux.build'

nix --experimental-features 'nix-command flakes' shell 'github:edolstra/dwarffs' --command dwarffs --version
#+end_src

#+results:
: fusermount version: 2.9.9

For full reproducibility, you can refer to specific point of a flake (e.g., commit).

#+begin_tip
To easily get a pinned URL, you can run =flake metadata= subcommand.

#+begin_src shell  :shebang "#!/usr/bin/env nix-shell"
#! nix-shell -i bash -p nixUnstable
nix --experimental-features 'nix-command flakes' flake metadata 'github:edolstra/dwarffs' | sed -e "s/\x1b\[.\{1,5\}m//g"
#+end_src

#+results:
#+begin_example
Resolved URL:  github:edolstra/dwarffs
Locked URL:    github:edolstra/dwarffs/f691e2c991e75edb22836f1dbe632c40324215c5
Description:   A filesystem that fetches DWARF debug info from the Internet on demand
Path:          /nix/store/769s05vjydmc2lcf6b02az28wsa9ixh1-source
Revision:      f691e2c991e75edb22836f1dbe632c40324215c5
Last modified: 2021-01-21 22:41:26
Inputs:
├───nix: github:NixOS/nix/6254b1f5d298ff73127d7b0f0da48f142bdc753c
│   ├───lowdown-src: github:kristapsdz/lowdown/1705b4a26fbf065d9574dce47a94e8c7c79e052f
│   └───nixpkgs: github:NixOS/nixpkgs/ad0d20345219790533ebe06571f82ed6b034db31
└───nixpkgs follows input 'nix/nixpkgs'
#+end_example
#+end_tip

#+begin_src shell  :shebang "#!/usr/bin/env nix-shell"
#! nix-shell -i bash -p nixUnstable
nix --experimental-features 'nix-command flakes' shell github:edolstra/dwarffs/f691e2c991e75edb22836f1dbe632c40324215c5 --command dwarffs --version
#+end_src

#+results:
: fusermount version: 2.9.9
