1
1
#+REVEAL_THEME: night
2
2
#+OPTIONS: toc:nil num:nil
3
3
#+TITLE: Nix Introduction
4
- #+AUTHOR: David Johnson & Kevin Quick
4
+ #+AUTHOR: Kevin Quick & David Johnson
5
5
* Sales Pitch
6
6
* Why use nix to install packages?
7
7
* Reproducible builds
76
76
* Roadmap
77
77
#+ATTR_REVEAL: :frag (none highlight-green highlight-green none none)
78
78
1. Define a pure language
79
- 2. Evaluate language to determine goals
80
- 3. Effect goals in IO
79
+ 2. Create Derivations
80
+ 3. Realize Derivations
81
81
4. Store results
82
82
5. Compose results into environments
83
- * nix effects (aka. IO)
84
- * core set of builders (aka. "stdenv")
85
- [[https://github.com/NixOS/nixpkgs/blob/master/pkgs/stdenv/generic/setup.sh][https://github.com/NixOS/nixpkgs/blob/master]]
86
- [[https://github.com/NixOS/nixpkgs/blob/master/pkgs/stdenv/generic/setup.sh][/pkgs/stdenv/generic/setup.sh]]
87
- * look for specific attributes and use them
88
- * builders
89
- * look for specific attributes
90
- * can define stages
91
- * invoke sub-builders
92
- * build specification
93
- * Determine build goal
83
+ * Create Derivations
84
+ * What is a Derivation?
85
+ * A derivation is the instructions on how to build something, and references to every dependency it has.
86
+ * Derivations are implemented as sets, but treated specially during evaluation.
87
+ - Perform side effects like producing build output
88
+ * derivation is the most important primitive in nix
89
+ - Requires a name
90
+ - Requires a builder (script for building the package)
91
+ - All attributes are passed as environment variables to the build.
92
+ - Requires a src (can use local source, or perform IO and fetch over network)
93
+ * In practice you'll use wrappers (i.e. runCommand, writeScriptBin, mkDerivation).
94
+ * mkDerivation uses setup.sh by default
95
+ - [[https://github.com/NixOS/nixpkgs/blob/master/pkgs/stdenv/generic/setup.sh][https://github.com/ixOS/nixpkgs/blob/master]][[https://github.com/NixOS/nixpkgs/blob/master/pkgs/stdenv/generic/setup.sh][/pkgs/stdenv/generic/setup.sh]]
96
+ * fetchUrl, fetchFromGitHub, fetchFromGitLab, etc. are fixed output derivations.
97
+ - Perform network IO, but "pure" because they perform "hash matching".
98
+ * Sample derivation
94
99
#+BEGIN_EXAMPLE
95
- $ nix-env -iA nix.pkgs.git
96
- #+END_EXAMPLE
97
- * Specifies the derivation to run the builder on
98
- #+BEGIN_SRC nix
99
- nix = { ...
100
- pkgs = { ...
101
- git = mkDerivation { name="git"; src=...; ...}
102
- ...
103
- };
100
+ # default parameter, lazily imports all of nixpkgs namespace into scope
101
+ { pkgs ? import <nixpkgs> {} }:
102
+ # stdenv is passed in automatically
103
+ pkgs.stdenv.mkDerivation {
104
+ # name
105
+ name = "hello-2.9";
106
+ # src itself is a derivation
107
+ # All derivations are stored in the /nix/store, before being realized
108
+ src = pkgs.fetchurl {
109
+ url = "mirror://gnu/hello/${name}.tar.gz";
110
+ sha256 = "19qy37gkasc4csb1d3bdiz9snn8mir2p3aj0jgzmfv0r2hi7mfzc";
111
+ };
104
112
}
105
- #+END_SRC
106
- * Here "nix" is actually a "channel" name (described later)
107
- * Roadmap
108
- #+ATTR_REVEAL: :frag (none none none highlight-green none)
109
- 1. Define a pure language
110
- 2. Evaluate language to determine goals
111
- 3. Effect goals in IO
112
- 4. Store results
113
- 5. Compose results into environments
114
- * nix-env command
115
- * Input is which derivation to run builders on
116
- * nix-env puts build output into the "store"
113
+ #+END_EXAMPLE
114
+ * nix store
115
+ * Building a derivation puts build output into the "store"
117
116
- System global directory tree
118
117
* Nothing is in scope
119
118
- dependencies passed in to derivation as args
120
119
- args reference store locations
121
120
- nix-env recursively builds dependencies
121
+ ** Unique hashes
122
122
* each store entry identified by hash
123
123
#+BEGIN_EXAMPLE
124
124
$ ls -CF /nix/store/q0crs4bg5vgl9cjpp9yxysd1w97inr0-git-2.13.2/
125
- bin/ etc/ lib/ libexec/ share/
125
+ bin/ etc/ lib/ libexec/ share/
126
126
#+END_EXAMPLE
127
- ** Unique hashes
128
127
* every package and every version has unique hash
129
128
* closure over build-time inputs
130
- #+BEGIN_EXAMPLE
131
- $ ls /nix/store/<TAB>
132
- Display all 7644 possibilities? (y or n)
133
- #+END_EXAMPLE
134
129
* can have multiple versions
135
130
* each store location is isolated
136
131
* all dependencies are _explicit_ in the derivation
137
- ** Sample specification
138
- #+BEGIN_SRC nix
139
- { stdenv, fetchurl, pkgconfig, libgphoto2, libexif, popt, gettext
140
- , libjpeg, readline, libtool
141
- }:
142
-
143
- stdenv.mkDerivation rec {
144
- name = "gphoto2-2.5.11";
145
-
146
- src = fetchurl {
147
- url = "mirror://sourceforge/gphoto/${name}.tar.bz2";
148
- sha256 = "1sgr6rsvzzagcwhc8fxbnvz3k02wr2hab0vrbvcb04k5l3b48a1r";
149
- };
150
-
151
- nativeBuildInputs = [ pkgconfig gettext libtool ];
152
- buildInputs = [ libgphoto2 libexif popt libjpeg readline ];
153
-
154
- meta = with stdenv.lib; {
155
- description = "A ready to use set of digital camera software applications";
156
- homepage = http://www.gphoto.org/;
157
- license = licenses.gpl2Plus;
158
- platforms = platforms.unix;
159
- maintainers = [ maintainers.jcumming ];
160
- };
161
- }
162
- #+END_SRC
132
+ * Realize derivations
133
+ * Phase distinction, instantiation & realization
134
+ * nix (https://github.com/nixos/nix)
135
+ - Heart of nix system, 4 C++ libraries, dozen or so utilities
136
+ * nix-instantiate
137
+ - Creates build instructions, calculates hash
138
+ - https://gist.github.com/dmjio/7e5a024f95fe18aa34f8fea69ecebc74#file-derivation-txt-L2
139
+ - https://github.com/NixOS/nix/blob/master/src/libstore/store-api.cc#L104
140
+ - Will exploit binary cache if store path exists on networked machines
141
+ * nix-store -r
142
+ - Attempts to build ("realize") package from derivation description
143
+ - Can watch evaluation through setup.sh by tracing "set -x"
144
+ * nix-build
145
+ - Equivalent to calling 'nix-store -r $(nix-instantiate default.nix)'
146
+ - Creates derivation, builds package, installs in store
147
+ * Builds go through phases
148
+ - pre/postFetch, pre/postBuild, pre/postInstall. etc/
149
+ - Package builds are split into phases to make it easier to override specific parts of the build (e.g., unpacking the sources or installing the binaries).
150
+ - New phases can be defined (haskell packages does this, i.e. haddockPhase)
151
+ - Default phase behavior can be seen in setup.sh
152
+ * nix-shell $(nix-instantiate default.nix)
153
+ - Useful for interactively building derivations
154
+ - Puts a user into a shell with environment variables from the derivation present
155
+ - Can run the build with `genericBuild`
156
+ * Roadmap
157
+ #+ATTR_REVEAL: :frag (none none none highlight-green none)
158
+ 1. Define a pure language
159
+ 2. Evaluate language to determine goals
160
+ 3. Effect goals in IO
161
+ 4. Store results
162
+ 5. Compose results into environments
163
163
* Roadmap
164
164
#+ATTR_REVEAL: :frag (none none none none highlight-green)
165
165
1. Define a pure language
@@ -183,7 +183,7 @@ stdenv.mkDerivation rec {
183
183
- nix-shell
184
184
- shell with build environment, but doesn't build current
185
185
package
186
- * nix pkgs
186
+ * nixpkgs
187
187
* A large set of ready-made derivations
188
188
* Each function is (ultimately) the mkDerivation
189
189
* channels: tarball of packages
@@ -256,6 +256,33 @@ stdenv.mkDerivation rec {
256
256
https://github.com/Gabriel439/haskell-nix
257
257
* Also John Wiegley's work:
258
258
https://github.com/jwiegley/nix-config
259
+ ** Sample specification
260
+ #+BEGIN_SRC nix
261
+ { stdenv, fetchurl, pkgconfig, libgphoto2, libexif, popt, gettext
262
+ , libjpeg, readline, libtool
263
+ }:
264
+
265
+ stdenv.mkDerivation rec {
266
+ name = "gphoto2-2.5.11";
267
+
268
+ src = fetchurl {
269
+ url = "mirror://sourceforge/gphoto/${name}.tar.bz2";
270
+ sha256 = "1sgr6rsvzzagcwhc8fxbnvz3k02wr2hab0vrbvcb04k5l3b48a1r";
271
+ };
272
+
273
+ nativeBuildInputs = [ pkgconfig gettext libtool ];
274
+ buildInputs = [ libgphoto2 libexif popt libjpeg readline ];
275
+
276
+ meta = with stdenv.lib; {
277
+ description = "A ready to use set of digital camera software applications";
278
+ homepage = http://www.gphoto.org/;
279
+ license = licenses.gpl2Plus;
280
+ platforms = platforms.unix;
281
+ maintainers = [ maintainers.jcumming ];
282
+ };
283
+ }
284
+ #+END_SRC
285
+
259
286
* REPL
260
287
* REPL tool for language and inspection
261
288
#+BEGIN_EXAMPLE
@@ -280,6 +307,16 @@ stdenv.mkDerivation rec {
280
307
- Filesystem dirent refcnts determine when removeable.
281
308
* Prefer nix-shell to nix-env
282
309
- Keep your main environment minimal
310
+ * Examples
311
+ - CyberChaff
312
+ - Build
313
+ - https://gist.github.com/e6ad693c41b49b925557fd52aaaf6a42
314
+ - DDOS-gui (http://ddos.dmj.io)
315
+ - Build
316
+ - https://gist.github.com/dmjio/3a1d977fe9e537ca2ad6efcd3a708d4b
317
+ - NixOS module, deployment
318
+ - https://gist.github.com/dmjio/7c0cb81113c7c46cf35edceb5c7960ac
319
+ - https://gist.github.com/dmjio/7705f8c112506f8e93ee09271ccdd14e
283
320
* THE END
284
321
* Nix language
285
322
- https://nixos.org/nix/manual
0 commit comments