Update Nix-related notes

Pretty incomplete but it is going to be... :)
This commit is contained in:
foo-dogsquared 2021-12-28 21:13:35 +08:00
parent 6200120c94
commit ffbd770528
4 changed files with 309 additions and 95 deletions

View File

@ -3,7 +3,7 @@
:END: :END:
#+title: Nix language #+title: Nix language
#+date: "2021-06-30 13:30:19 +08:00" #+date: "2021-06-30 13:30:19 +08:00"
#+date_modified: "2021-07-19 11:30:52 +08:00" #+date_modified: "2021-12-28 00:17:58 +08:00"
#+language: en #+language: en
@ -18,18 +18,24 @@ This is just a personal reference note, the recommended resource for learning th
* What is it for? * What is it for?
- most importantly, writing packages - most importantly, writing packages, specifically [[id:8f23f862-a19a-4a13-8d8f-69c280a8e072][Nix derivations]]
- mostly used for configuring [[https://nixos.org/manual/nixos/stable/][operating systems]] and create servers quickly with its variety of services - mostly used for configuring [[https://nixos.org/manual/nixos/stable/][operating systems]] and create servers quickly with its variety of services
- configuring [[id:3b3fdcbf-eb40-4c89-81f3-9d937a0be53c][Nix package manager]] - configuring [[id:3b3fdcbf-eb40-4c89-81f3-9d937a0be53c][Nix package manager]]
* Derivations * Builtins
- ~builtins.toString ./.~ will print out the current directory.
- ~builtins.getFlake $PATH~
** Imports
- the =import= builtin will simply import the Nix expression given a path.
- because of the functional paradigm, you'll often see the builtin used with the parameters in one go — e.g., ~import <nixpkgs> { }~ which will import nixpkgs from one of the module paths with the default attribute set.
- [[id:8f23f862-a19a-4a13-8d8f-69c280a8e072][Nix derivations]] are one of the central components of the package manager
- =mkDerivation=
- derivations are more verbose than necessary, most of the packaging process are done with the =nixpkgs= which contains a standard library to easily create derivations with different setups from programming languages and frameworks
@ -37,4 +43,4 @@ This is just a personal reference note, the recommended resource for learning th
- nixpkgs has a standard library that comes with functions mitigating against the verbosity for packaging or simply convenience - nixpkgs has a standard library that comes with functions mitigating against the verbosity for packaging or simply convenience
* TODO Imports -

View File

@ -3,7 +3,7 @@
:END: :END:
#+title: Nix packages #+title: Nix packages
#+date: 2021-07-18 21:23:26 +08:00 #+date: 2021-07-18 21:23:26 +08:00
#+date_modified: 2021-07-18 21:23:26 +08:00 #+date_modified: 2021-12-12 23:48:08 +08:00
#+language: en #+language: en
@ -13,3 +13,22 @@
- for certain setups such as common conventions for certain programming languages like Rust and Go, nixpkgs has [[https://nixos.org/manual/nixpkgs/unstable/#chap-language-support][environments created for those]]; - for certain setups such as common conventions for certain programming languages like Rust and Go, nixpkgs has [[https://nixos.org/manual/nixpkgs/unstable/#chap-language-support][environments created for those]];
different versions may have changes so be sure to go into the appropriate documentation and create packages for a specific channel; different versions may have changes so be sure to go into the appropriate documentation and create packages for a specific channel;
this is significantly easier with [[id:6873de22-9eac-492c-93a8-6cdf8cbfc0f8][Nix flakes]] this is significantly easier with [[id:6873de22-9eac-492c-93a8-6cdf8cbfc0f8][Nix flakes]]
* Ecosystems
- while derivations can be interacted with the builtin function =builtins.derivation=, it isn't really recommended since it is very verbose;
instead, you use =stdenv.mkDerivation= from nixpkgs
- nixpkgs is the official repository of NixOS containing the standard library extending the [[id:a57e63a7-6daa-4639-910d-c6648df156a3][Nix language]], the largest repository of packages (counting up to 80,000 as of 2021-12-12), and various NixOS modules used to build the system itself
- nixpkgs contains =stdenv= which is a derivation meant to be a base for other packages
- stdenv contains base packages that virtually all packages have (inside nixpkgs)
-
- =stdenv.mkDerivation= is a convenient way of creating derivations;
however, it is very abstract and a lot of events happening around it
- this is a function that accepts a large attribute set
- it will map its arguments corresponding to a parameter in the final derivation made with =stdenv= (which is a shell script)
- attributes are usually just variables;
this is especially prevelant if
- packages can also set setup hooks (i.e., =setupHook=) that are essentially shell scripts to be added in the final derivation
- example: [[https://github.com/NixOS/nixpkgs/tree/nixos-21.11/pkgs/development/tools/build-managers/cmake][cmake]], [[https://github.com/NixOS/nixpkgs/tree/nixos-21.11/pkgs/development/libraries/glib][glib]], and [[https://github.com/NixOS/nixpkgs/tree/nixos-21.11/pkgs/development/tools/build-managers][most of the build systems at nixpkgs]]

View File

@ -3,7 +3,7 @@
:END: :END:
#+title: Nix derivations #+title: Nix derivations
#+date: 2021-07-18 23:16:39 +08:00 #+date: 2021-07-18 23:16:39 +08:00
#+date_modified: 2021-07-18 23:16:39 +08:00 #+date_modified: 2021-12-28 00:16:07 +08:00
#+language: en #+language: en
@ -13,41 +13,41 @@ At a glance, this is the equivalent to manifests from [[id:ecee1a61-3d5c-4c8f-a2
The following code block shows what a derivation looks like. The following code block shows what a derivation looks like.
#+begin_src bash :cache yes #+begin_src bash :cache yes
nix show-derivation nixpkgs.hello nix show-derivation nixpkgs#hello
#+end_src #+end_src
#+results[e489b584c0de1eac206a48340452807005a9bf87]: #+results[41a57682cb5744db1bd748068384a6d4bc4702c5]:
#+begin_example #+begin_example
{ {
"/nix/store/7xcq1j6kxry9p5scmgccifqp1m57ha17-hello-2.10.drv": { "/nix/store/vvb4wxmnjixmrkhmj2xb75z62hrr41i7-hello-2.10.drv": {
"outputs": { "outputs": {
"out": { "out": {
"path": "/nix/store/kzq2f6pqb3ig89278n3c21g6x4y3pghs-hello-2.10" "path": "/nix/store/xcp9cav49dmsjbwdjlmkjxj10gkpx553-hello-2.10"
} }
}, },
"inputSrcs": [ "inputSrcs": [
"/nix/store/9krlzvny65gdc8s7kpb6lkx8cd02c25b-default-builder.sh" "/nix/store/9krlzvny65gdc8s7kpb6lkx8cd02c25b-default-builder.sh"
], ],
"inputDrvs": { "inputDrvs": {
"/nix/store/aq6m16jyrdpz2frivygw0502lkhyv271-stdenv-linux.drv": [ "/nix/store/127p1886lb1q7x02wyy77ib9ghhc324v-hello-2.10.tar.gz.drv": [
"out" "out"
], ],
"/nix/store/hhq1cbwwjm28bp7bnr9ivlvgah7988xb-hello-2.10.tar.gz.drv": [ "/nix/store/4lzwc3wzbqjq973psxkph8jjq4g6cssr-stdenv-linux.drv": [
"out" "out"
], ],
"/nix/store/mhgg30w6ayhbvnp03z8gx4c92n67ajg6-bash-4.4-p23.drv": [ "/nix/store/js6ib8zv84knb7kwnjdrqgwf86djjblk-bash-5.1-p12.drv": [
"out" "out"
] ]
}, },
"platform": "x86_64-linux", "system": "x86_64-linux",
"builder": "/nix/store/26a78ync552m8j4sbjavhvkmnqir8c9y-bash-4.4-p23/bin/bash", "builder": "/nix/store/a54wrar1jym1d8yvlijq0l2gghmy8szz-bash-5.1-p12/bin/bash",
"args": [ "args": [
"-e", "-e",
"/nix/store/9krlzvny65gdc8s7kpb6lkx8cd02c25b-default-builder.sh" "/nix/store/9krlzvny65gdc8s7kpb6lkx8cd02c25b-default-builder.sh"
], ],
"env": { "env": {
"buildInputs": "", "buildInputs": "",
"builder": "/nix/store/26a78ync552m8j4sbjavhvkmnqir8c9y-bash-4.4-p23/bin/bash", "builder": "/nix/store/a54wrar1jym1d8yvlijq0l2gghmy8szz-bash-5.1-p12/bin/bash",
"configureFlags": "", "configureFlags": "",
"depsBuildBuild": "", "depsBuildBuild": "",
"depsBuildBuildPropagated": "", "depsBuildBuildPropagated": "",
@ -61,14 +61,14 @@ nix show-derivation nixpkgs.hello
"doInstallCheck": "", "doInstallCheck": "",
"name": "hello-2.10", "name": "hello-2.10",
"nativeBuildInputs": "", "nativeBuildInputs": "",
"out": "/nix/store/kzq2f6pqb3ig89278n3c21g6x4y3pghs-hello-2.10", "out": "/nix/store/xcp9cav49dmsjbwdjlmkjxj10gkpx553-hello-2.10",
"outputs": "out", "outputs": "out",
"patches": "", "patches": "",
"pname": "hello", "pname": "hello",
"propagatedBuildInputs": "", "propagatedBuildInputs": "",
"propagatedNativeBuildInputs": "", "propagatedNativeBuildInputs": "",
"src": "/nix/store/3x7dwzq014bblazs7kq20p9hyzz0qh8g-hello-2.10.tar.gz", "src": "/nix/store/3x7dwzq014bblazs7kq20p9hyzz0qh8g-hello-2.10.tar.gz",
"stdenv": "/nix/store/dj40kjgp5lhs55v4hc47vyrarhq4qycz-stdenv-linux", "stdenv": "/nix/store/xcmlbsqabmckx42p8w18ri5zq8v2iiav-stdenv-linux",
"strictDeps": "", "strictDeps": "",
"system": "x86_64-linux", "system": "x86_64-linux",
"version": "2.10" "version": "2.10"
@ -77,9 +77,7 @@ nix show-derivation nixpkgs.hello
} }
#+end_example #+end_example
While we can create derivations with the [[id:a57e63a7-6daa-4639-910d-c6648df156a3][Nix language]], it is unnecessary and verbose if we use nixpkgs. While we can create derivations with the =derivations= builtin from the [[id:a57e63a7-6daa-4639-910d-c6648df156a3][Nix language]], in practice you'll use =stdenv.mkDerivation= from nixpkgs standard library.
nixpkgs has made the process easier with their standard library.
- =stdenv.mkDerivation=
- nixpkgs also comes with several convenient functions/environments for several programming languages and frameworks including Python, Rust, Go, and Nim - nixpkgs also comes with several convenient functions/environments for several programming languages and frameworks including Python, Rust, Go, and Nim
- it also includes environments for common setups such as for clang compiler - it also includes environments for common setups such as for clang compiler

View File

@ -3,7 +3,7 @@
:END: :END:
#+title: Nix flakes #+title: Nix flakes
#+date: 2021-07-18 22:34:11 +08:00 #+date: 2021-07-18 22:34:11 +08:00
#+date_modified: 2021-09-14 18:39:43 +08:00 #+date_modified: 2021-12-28 21:12:39 +08:00
#+language: en #+language: en
@ -11,96 +11,287 @@
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. 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 #+end_note
- similar to [[https://guix.gnu.org/manual/en/html_node/Channels.html][Guix channels]] What are Nix flakes?
- 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? A self-contained Nix module that takes inputs (can be from other flakes) and create outputs.
It is comparable to [[https://guix.gnu.org/manual/en/html_node/Channels.html][Guix channels]] (or the NUR) in the way any arbitrary value can be exported, thus extending a Nix module in any direction the author can make it to.
Also, it supercedes Nix channels as a way to manage your system.
- provides a structure for discoverability So, why use flakes?
- 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. - Since it is self-contained, it is easier to compose and develop environments for projects.
It will show the entire outputs of a flake as well as the normalized version of the flake object. - It is more declarative compared to Nix channels.
- Provides a structure for discoverability through exploring the output.
- In case you're using roam:NixOS, it also provides an easier way to extend it with third-party custom modules.
#+name: flake-sample-object
#+begin_src python :results value silent :exports none
return "github:edolstra/dwarffs"
* Exploring a Nix flake
To get started with a flake, you can quickly create one with =nix flake init=.
This will create =flake.nix= which is also required for Nix to recognize the project as a flake.
Here's the skeleton of a flake.
#+begin_src nix
{
description = "A basic flake.";
inputs = {};
outputs = {};
}
#+end_src #+end_src
#+begin_src shell :shebang "#!/usr/bin/env nix-shell" It is really just an attribute set that mainly deals with three attributes:
#! 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" - =description= which is self-descriptive enough to see what's it for. ;p
- =inputs= which contains inputs (that are other flakes) to be used for...
- ...the =outputs= which is a function that returns an attribute set to be exported.
Here's a real example of a basic flake.
#+begin_src nix
{
description = "A basic flake with some real inputs this time.";
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs";
home-manager.url = "github:nix-community/home-manager";
home-manager.inputs.nixpkgs.follows = "nixpkgs";
};
outputs = attrs@{ self, nixpkgs, home-manager, ... }: {
homeManagerConfiguration = import ./hmUsers;
packages.x86_64-linux = {
hello = nixpkgs.pkgs.hello;
};
};
}
#+end_src #+end_src
#+results: The flake requires other flakes from the nixpkgs and home-manager as indicated from the URL.
This will get the latest revision of both inputs.
This is not ideal as it will get the latest revisions every time it requests the inputs.
While you can pin the version by adding more information (e.g., =nixpkgs.url = "github:NixOS/nixpkgs/nixos-21.11"=, =home-manager.url = "github:nix-community/home-manager/781d25b315def05cd7ede3765226c54216f0b1fe"=), this isn't really necessary for the most part as we have lockfiles to secure our dependencies version.
These lockfiles can be found at =flake.lock= where complete metadata for the flakes are found.
* Selected list of attributes for outputs
While you can export attributes of any type, there are common attributes you'll mostly see.
As an example, here's the flake of my NixOS configuration.
#+begin_src shell :cache yes :results output
nix flake show github:foo-dogsquared/nixos-config | nix run nixpkgs#gnused -- 's/\x1b\[[0-9;]*m//g'
#+end_src
#+results[cf6df0202da35fbba99f7581b2655fb75d9eef8a]:
#+begin_example #+begin_example
github:edolstra/dwarffs/f691e2c991e75edb22836f1dbe632c40324215c5 github:foo-dogsquared/nixos-config/12077bfc601e5465e343e590a830e8d3cb6a1a59
├───checks ├───devShells
│ ├───aarch64-darwin
│ │ ├───flatpak: development environment 'nix-shell'
│ │ ├───hugo: development environment 'nix-shell'
│ │ └───rust: development environment 'nix-shell'
│ ├───aarch64-linux │ ├───aarch64-linux
│ │ ├───build: derivation 'dwarffs-0.1.20210121.f691e2c' │ │ ├───flatpak: development environment 'nix-shell'
│ │ └───test: derivation 'vm-test-run-unnamed' │ │ ├───hugo: development environment 'nix-shell'
│ │ └───rust: development environment 'nix-shell'
│ ├───i686-linux │ ├───i686-linux
│ │ ├───build: derivation 'dwarffs-0.1.20210121.f691e2c' │ │ ├───flatpak: development environment 'nix-shell'
│ │ └───test: derivation 'vm-test-run-unnamed' │ │ ├───hugo: development environment 'nix-shell'
│ │ └───rust: development environment 'nix-shell'
│ ├───x86_64-darwin
│ │ ├───flatpak: development environment 'nix-shell'
│ │ ├───hugo: development environment 'nix-shell'
│ │ └───rust: development environment 'nix-shell'
│ └───x86_64-linux │ └───x86_64-linux
│ ├───build: derivation 'dwarffs-0.1.20210121.f691e2c' │ ├───flatpak: development environment 'nix-shell'
│ └───test: derivation 'vm-test-run-unnamed' │ ├───hugo: development environment 'nix-shell'
├───defaultPackage │ └───rust: development environment 'nix-shell'
│ ├───aarch64-linux: package 'dwarffs-0.1.20210121.f691e2c' ├───homeManagerConfigurations: unknown
│ ├───i686-linux: package 'dwarffs-0.1.20210121.f691e2c' ├───homeManagerModules: unknown
│ └───x86_64-linux: package 'dwarffs-0.1.20210121.f691e2c' ├───lib: unknown
├───nixosConfigurations
│ └───ni: NixOS configuration
├───nixosModules ├───nixosModules
│ └───dwarffs: NixOS module │ ├───agenix: NixOS module
└───overlay: Nixpkgs overlay │ ├───archiving: NixOS module
│ ├───desktop: NixOS module
│ ├───dev: NixOS module
│ ├───hardware-setup: NixOS module
│ ├───themes: NixOS module
│ └───users: NixOS module
└───packages
├───aarch64-darwin
│ ├───doggo: package 'doggo-0.4.1'
│ ├───gnome-shell-extension-burn-my-windows: package 'gnome-shell-extension-burn-my-windows-2'
│ ├───gnome-shell-extension-desktop-cube: package 'gnome-shell-extension-desktop-cube-5'
│ ├───gnome-shell-extension-fly-pie: package 'gnome-shell-extension-fly-pie-12'
│ ├───gnome-shell-extension-pop-shell: package 'gnome-shell-extension-pop-shell-unstable-2021-11-30'
│ ├───libcs50: package 'libcs50-10.1.1'
│ ├───llama: package 'llama-1.0.2'
│ ├───neo: package 'neo-0.6'
│ ├───pop-launcher: package 'pop-launcher-1.1.0'
│ ├───pop-launcher-plugin-duckduckgo-bangs: package 'pop-launcher-plugin-duckduckgo-bangs-1.3.0'
│ ├───sioyek: package 'sioyek-1.0.0'
│ └───tic-80: package 'tic-80-unstable-2021-12-18'
├───aarch64-linux
│ ├───doggo: package 'doggo-0.4.1'
│ ├───gnome-shell-extension-burn-my-windows: package 'gnome-shell-extension-burn-my-windows-2'
│ ├───gnome-shell-extension-desktop-cube: package 'gnome-shell-extension-desktop-cube-5'
│ ├───gnome-shell-extension-fly-pie: package 'gnome-shell-extension-fly-pie-12'
│ ├───gnome-shell-extension-pop-shell: package 'gnome-shell-extension-pop-shell-unstable-2021-11-30'
│ ├───libcs50: package 'libcs50-10.1.1'
│ ├───llama: package 'llama-1.0.2'
│ ├───neo: package 'neo-0.6'
│ ├───pop-launcher: package 'pop-launcher-1.1.0'
│ ├───pop-launcher-plugin-duckduckgo-bangs: package 'pop-launcher-plugin-duckduckgo-bangs-1.3.0'
│ ├───sioyek: package 'sioyek-1.0.0'
│ └───tic-80: package 'tic-80-unstable-2021-12-18'
├───i686-linux
│ ├───doggo: package 'doggo-0.4.1'
│ ├───gnome-shell-extension-burn-my-windows: package 'gnome-shell-extension-burn-my-windows-2'
│ ├───gnome-shell-extension-desktop-cube: package 'gnome-shell-extension-desktop-cube-5'
│ ├───gnome-shell-extension-fly-pie: package 'gnome-shell-extension-fly-pie-12'
│ ├───gnome-shell-extension-pop-shell: package 'gnome-shell-extension-pop-shell-unstable-2021-11-30'
│ ├───libcs50: package 'libcs50-10.1.1'
│ ├───llama: package 'llama-1.0.2'
│ ├───neo: package 'neo-0.6'
│ ├───pop-launcher: package 'pop-launcher-1.1.0'
│ ├───pop-launcher-plugin-duckduckgo-bangs: package 'pop-launcher-plugin-duckduckgo-bangs-1.3.0'
│ ├───sioyek: package 'sioyek-1.0.0'
│ └───tic-80: package 'tic-80-unstable-2021-12-18'
├───x86_64-darwin
│ ├───doggo: package 'doggo-0.4.1'
│ ├───gnome-shell-extension-burn-my-windows: package 'gnome-shell-extension-burn-my-windows-2'
│ ├───gnome-shell-extension-desktop-cube: package 'gnome-shell-extension-desktop-cube-5'
│ ├───gnome-shell-extension-fly-pie: package 'gnome-shell-extension-fly-pie-12'
│ ├───gnome-shell-extension-pop-shell: package 'gnome-shell-extension-pop-shell-unstable-2021-11-30'
│ ├───libcs50: package 'libcs50-10.1.1'
│ ├───llama: package 'llama-1.0.2'
│ ├───neo: package 'neo-0.6'
│ ├───pop-launcher: package 'pop-launcher-1.1.0'
│ ├───pop-launcher-plugin-duckduckgo-bangs: package 'pop-launcher-plugin-duckduckgo-bangs-1.3.0'
│ ├───sioyek: package 'sioyek-1.0.0'
│ └───tic-80: package 'tic-80-unstable-2021-12-18'
└───x86_64-linux
├───doggo: package 'doggo-0.4.1'
├───gnome-shell-extension-burn-my-windows: package 'gnome-shell-extension-burn-my-windows-2'
├───gnome-shell-extension-desktop-cube: package 'gnome-shell-extension-desktop-cube-5'
├───gnome-shell-extension-fly-pie: package 'gnome-shell-extension-fly-pie-12'
├───gnome-shell-extension-pop-shell: package 'gnome-shell-extension-pop-shell-unstable-2021-11-30'
├───libcs50: package 'libcs50-10.1.1'
├───llama: package 'llama-1.0.2'
├───neo: package 'neo-0.6'
├───pop-launcher: package 'pop-launcher-1.1.0'
├───pop-launcher-plugin-duckduckgo-bangs: package 'pop-launcher-plugin-duckduckgo-bangs-1.3.0'
├───sioyek: package 'sioyek-1.0.0'
└───tic-80: package 'tic-80-unstable-2021-12-18'
#+end_example #+end_example
Let's build from one of the outputs of call_flake-sample-object(). - =defaultPackage.${system}.${package}= (or =packages.${system}.${package}=) is mainly expected for packages.
This allows for easy building — e.g., =nix build nixpkgs#hello= will refer to =defaultPackage.${system}.hello=.
Another command that expects this is =nix run ${PACKAGE}= (e.g., =nix run nixpkgs#hello=).
#+begin_src shell :shebang "#!/usr/bin/env nix-shell" - =nixosConfigurations.${host}= is a NixOS host configuration.
#! nix-shell -i bash -p nixUnstable Each attribute contain a set similar to the traditional set from =/etc/nixos/configuration.nix=.
nix --experimental-features 'nix-command flakes' build 'github:edolstra/dwarffs#checks.aarch64-linux.build' This is very beneficial for quickly installing only with flakes — e.g., =nixos-install --flake github:foo-dogsquared/nixos-config#zilch= will install with =nixosConfigurations.zilch=.
However, attributes should be created with =lib.nixosSystem= from =nixpkgs= flake.
nix --experimental-features 'nix-command flakes' shell 'github:edolstra/dwarffs' --command dwarffs --version - =nixosModules.${module}= is a [[roam:NixOS modules]], allowing you to extend NixOS further.
These are expected to be similar NixOS modules from nixpkgs.
- =templates.${name}= is a template that has the attribute =path= and =description=.
See [[Flake templates]] for more details about how it's used.
- =devShell.${system}= is expected to be a derivation (mostly with =mkShell=).
This is the default development environment to be used.
This is mostly expected for projects providing an easy way to bootstrap for development (e.g., with =nix develop ${FLAKE}=).
- =devShells.${system}.${name}= is an attribute set of derivations (mostly from =mkShell=).
This is similar to =devShell= except this is where =nix develop= subcommand finds if an attribute name is given.
* Flake templates
- Flakes can have templates to get started with.
They can be used with =nix flake init ${TEMPLATE}=.
- You can export it in your flakes through the =templates= attribute.
=templates= is expected to be an attribute set with each attribute representing a template.
- By default, we have the =templates= flake from the global registry pointed to [[https://github.com/NixOS/templates][NixOS/templates]] Git repo.
* Nix registry
Per the Nix manual:
#+begin_quote
Flake registries are a convenience feature that allows you to refer to flakes using symbolic identifiers such as =nixpkgs=, rather than full URLs such as =git://github.com/NixOS/nixpkgs=.
#+end_quote
Here's an example of the registry list with some overriden flakes such as the =nixpkgs= flake following from my [[https://github.com/foo-dogsquared/nixos-config][NixOS configuration]].
#+begin_src shell :cache yes
nix registry list
#+end_src #+end_src
#+results: #+results[2e793be4dcc8dcc2f0c921da5dffa1544455d14e]:
: 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 #+begin_example
Resolved URL: github:edolstra/dwarffs system flake:agenix path:/nix/store/yka795vkb7ny5fnybf8dafbypcjfmi9n-source?lastModified=1638837456&narHash=sha256-WHLOxthAGx%2fwXw3QUa%2flFE3mr6cQtnXfFYZ0DNyYwt4=&rev=57806bf7e340f4cae705c91748d4fdf8519293a9
Locked URL: github:edolstra/dwarffs/f691e2c991e75edb22836f1dbe632c40324215c5 system flake:config path:/nix/store/3nlagaj6748w4ffxx4vp5jss2k571f8i-source?lastModified=1640442672&narHash=sha256-Gkt2On9szrFlOo6QiYMOA90qTp4PICd7STHFhGA4bCs=
Description: A filesystem that fetches DWARF debug info from the Internet on demand system flake:home-manager path:/nix/store/ijh6v700kpssfyw44v4awbm2gmjx26qs-source?lastModified=1640296831&narHash=sha256-Mu32vTcfZru4VrvgnpvQKmwC4uY0oF3vnnC2o9SgnRU=&rev=f15b151ca1c4aea23515c241051d71f1b5cf97c8
Path: /nix/store/769s05vjydmc2lcf6b02az28wsa9ixh1-source system flake:nixpkgs path:/nix/store/lgfhg4n6445yizgf0xjirb4bc4j86fr9-source?lastModified=1640269308&narHash=sha256-vBVwv3+kPrxbNyfo48cB5cc5%2f4tq5zlJGas%2fqw8XNBE=&rev=0c408a087b4751c887e463e3848512c12017be25
Revision: f691e2c991e75edb22836f1dbe632c40324215c5 global flake:agda github:agda/agda
Last modified: 2021-01-21 22:41:26 global flake:blender-bin github:edolstra/nix-warez?dir=blender
Inputs: global flake:dreampkgs github:nix-community/dreampkgs
├───nix: github:NixOS/nix/6254b1f5d298ff73127d7b0f0da48f142bdc753c global flake:dwarffs github:edolstra/dwarffs
│ ├───lowdown-src: github:kristapsdz/lowdown/1705b4a26fbf065d9574dce47a94e8c7c79e052f global flake:fenix github:nix-community/fenix
│ └───nixpkgs: github:NixOS/nixpkgs/ad0d20345219790533ebe06571f82ed6b034db31 global flake:flake-utils github:flake-utils/numtide
└───nixpkgs follows input 'nix/nixpkgs' global flake:gemini github:nix-community/flake-gemini
global flake:home-manager github:nix-community/home-manager
global flake:hydra github:NixOS/hydra
global flake:mach-nix github:DavHau/mach-nix
global flake:nimble github:nix-community/flake-nimble
global flake:nix github:NixOS/nix
global flake:nixops github:NixOS/nixops
global flake:nixos-hardware github:NixOS/nixos-hardware
global flake:nixos-homepage github:NixOS/nixos-homepage/flake
global flake:nur github:nix-community/NUR
global flake:nixpkgs github:NixOS/nixpkgs/nixpkgs-unstable
global flake:templates github:NixOS/templates
global flake:patchelf github:NixOS/patchelf
global flake:nix-serve github:edolstra/nix-serve
global flake:nickel github:tweag/nickel
#+end_example #+end_example
#+end_tip
#+begin_src shell :shebang "#!/usr/bin/env nix-shell" So how does a flake registry work?
#! nix-shell -i bash -p nixUnstable
nix --experimental-features 'nix-command flakes' shell github:edolstra/dwarffs/f691e2c991e75edb22836f1dbe632c40324215c5 --command dwarffs --version
#+end_src
#+results: - It is managed through =nix registry= subcommand or set =nix.registry= in your system configuration.
: fusermount version: 2.9.9
- Registries are primarily written as JSON files in certain files (e.g., =$XDG_CONFIG_HOME/nix/registry=, =/etc/nix/registry.json=).
For more information, see the [[https://nixos.org/manual/nix/stable/command-ref/new-cli/nix3-registry.html#registry-format][registry format from the manual]].
Unlike the traditional Nix channels, the inclusion of arbitrary files and their locations doesn't seem to affect the reproducibility since it is mostly used as a convenience feature after all.
- The flakes from default registry are mostly getting the latest revisions of the flake per invocation so it is best practice to pin them (e.g., =nix registry pin=, through =nix.registry= while setting the NixOS systems in =flake.nix=).
- There are primarily three registries to worry about: user, system, and global.
- This is also the reason it downloads something why each time you invoke a Nix-related command (e.g., =nix search=, =nix edit=).
[fn:: I think pinning the flakes from the global registry will simply resolve this issue.]
* More information about flakes
- We can modify our inputs.
In the above example, we made =home-manager= to use our version of =nixpkgs= which will make it easier to sync.