You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
> g = import myfun.nix
> g { i=9; j=a; }
10
> c = {i=9;}
> g (c // { j=a; })
10
“inherit x y;” is syntactic sugar for “x=x; y=y;”
> j = a
> g { inherit j; i=9; } == g { i=9; j=j; }
true
Roadmap
Define a pure language
Evaluate language to determine goals: derivations
Storing builds
Realize Derivations (IO)
Compose results into environments
Create Derivations
What is a Derivation?
instructions on how to build something + references to every dependency
Implemented as sets, but treated specially during evaluation.
Perform side effects like producing build output
derivation is the most important primitive in nix
Requires a name
Requires a builder (script for building the package)
All attributes are passed as environment variables to the build.
Requires a src
can use local source, or perform IO and fetch over network
Writing Derivations
In practice you’ll use wrappers
e.g., runCommand, writeScriptBin, mkDerivation
mkDerivation uses setup.sh by default
fetchUrl, fetchFromGitHub, fetchFromGitLab, etc.
fixed output derivations.
Perform network IO, but “pure” because they perform “hash
matching”.
Sample derivation
{ pkgs ? import <nixpkgs> {} }:
# pkgs parameter has a default; it lazily imports
# all of nixpkgs namespace into scope
pkgs.stdenv.mkDerivation {
name = "hello-2.9";
# src itself is a derivation
# All derivations are stored in the /nix/store, before being realized
src = pkgs.fetchurl {
url = "mirror://gnu/hello/${name}.tar.gz";
sha256 = "19qy37gkasc4csb1d3bdiz9snn8mir2p3aj0jgzmfv0r2hi7mfzc";
};
};
}
Roadmap
Define a pure language
Evaluate language to determine goals: derivations
Storing builds
Realize Derivations (IO)
Compose results into environments
nix store
Building a derivation puts build output into the “store”
System global directory tree
Nothing is in scope
dependencies passed in to derivation as args
args reference store locations
Unique hashes
each store entry identified by hash
$ ls -CF /nix/store/q0crs4bg5vgl9cjpp9yxysd1w97inr0-git-2.13.2/
bin/ etc/ lib/ libexec/ share/
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).
New phases can be defined (haskell packages does this,
e.g. haddockPhase)
Default phase behavior can be seen in setup.sh
nix-shell
$ nix-shell $(nix-instantiate default.nix)
Useful for interactively building derivations
Puts a user into a shell with environment variables from the
derivation present
Can run the build with `genericBuild`
Roadmap
Define a pure language
Evaluate language to determine goals: derivations
Storing builds
Realize derivations (IO)
Compose results into environments
profiles expose packages
Each exe or lib has a unique store location
Want a useable environment to run multiple tools
nix-env creates profiles
Minimizing for Effectiveness
Conventional package managers install globally
This is an anti-pattern using nix
nix installs into global store, and then exposes locally
use only for generics, like vi and emacs
let projects specify the packages and versions needed
nix-build
local ”result” symlink instead of profile
nix-shell
shell with build environment, but doesn’t build current
package
{stdenv,fetchurl,pkgconfig,libgphoto2,libexif,popt,gettext,libjpeg,readline,libtool}:
stdenv.mkDerivationrec{name="gphoto2-2.5.11";src=fetchurl{url="mirror://sourceforge/gphoto/${name}.tar.bz2";sha256="1sgr6rsvzzagcwhc8fxbnvz3k02wr2hab0vrbvcb04k5l3b48a1r";};nativeBuildInputs=[pkgconfiggettextlibtool];buildInputs=[libgphoto2libexifpoptlibjpegreadline];meta=withstdenv.lib;{description="A ready to use set of digital camera software applications";homepage=http://www.gphoto.org/;license=licenses.gpl2Plus;platforms=platforms.unix;maintainers=[maintainers.jcumming];};}
REPL
REPL tool for language and inspection
$ nix-repl
> :l <nixpkgs>
> pkgs.git.m<TAB>
pkgs.git.makeFlags pkgs.git.meta
> pkgs.git.meta.description
"Distributed version control system"
Miscellaneous
Local builds are not incremental
Attributes are specific but not versioned
When needed, a new version-specific attribute is created