Nix shell and build dependencies

A nix-shell can be used to quickly set up ad-hoc development environments with all required dependencies in place.

SDL2 program

To compile an SDL2 program –for this example Tsoding's sowon is used– put the following in a shell.nix file:

 1with import <nixpkgs> {};
 2
 3let
 4    stdenv8 = overrideCC stdenv gcc8;
 5in
 6    stdenv8.mkDerivation rec {
 7        name = "sowon-build";
 8        env = buildEnv {
 9            name = name;
10            paths = buildInputs;
11        };
12        buildInputs = [
13            pkgconfig
14            SDL2
15        ];
16    }

Then start a Nix shell, which automatically gets all the dependencies, as follows:

 1$ nix-shell
 2these derivations will be built:
 3  /nix/store/z3cd8qw52a9b11h2izr6ms6vbifqc7w7-builder.pl.drv
 4  /nix/store/ms1xzcqrf37174q21l3jbwjz74m3fgib-sowon-build.drv
 5these paths will be fetched (34.44 MiB download, 136.10 MiB unpacked):
 6  /nix/store/71n1xcigc00w3z7yc836jqcx9cb2dys8-patchelf-0.9
 7  /nix/store/8bjj5zffqd7abvflgif7nl4vikmdp0jr-isl-0.17.1
 8  /nix/store/90v0fyf5flnd5xc4fysx4q4c9y21gpf1-stdenv-linux
 9  /nix/store/a9i1fi1if10pdvwmr44c2nwkd3lzy191-expand-response-params
10  /nix/store/cc0lmsl3b94xj5wcvm3h34qdcy8z2kzc-gcc-8.3.0
11  /nix/store/ckp85diipcr67cpmdablnih9fnrbjyyw-libmpc-1.1.0
12  /nix/store/d43v6bx7r6fcaq3fbbfd5mwh1f5s7rmg-bash-interactive-4.4-p23-dev
13  /nix/store/h53vw6vl9xq19x5y4prlwlfbpvnzh46s-gcc-8.3.0-lib
14  /nix/store/l2abq8hpgdjc4x7dwdps7zqcnxmjmjp4-gcc-wrapper-8.3.0
15  /nix/store/n4p9yimjanbkxiwlx3z0csacjlavb9sn-stdenv-linux
16copying path '/nix/store/d43v6bx7r6fcaq3fbbfd5mwh1f5s7rmg-bash-interactive-4.4-p23-dev' from 'https://cache.nixos.org'...
17copying path '/nix/store/a9i1fi1if10pdvwmr44c2nwkd3lzy191-expand-response-params' from 'https://cache.nixos.org'...
18copying path '/nix/store/h53vw6vl9xq19x5y4prlwlfbpvnzh46s-gcc-8.3.0-lib' from 'https://cache.nixos.org'...
19copying path '/nix/store/71n1xcigc00w3z7yc836jqcx9cb2dys8-patchelf-0.9' from 'https://cache.nixos.org'...
20copying path '/nix/store/8bjj5zffqd7abvflgif7nl4vikmdp0jr-isl-0.17.1' from 'https://cache.nixos.org'...
21copying path '/nix/store/ckp85diipcr67cpmdablnih9fnrbjyyw-libmpc-1.1.0' from 'https://cache.nixos.org'...
22copying path '/nix/store/n4p9yimjanbkxiwlx3z0csacjlavb9sn-stdenv-linux' from 'https://cache.nixos.org'...
23building '/nix/store/z3cd8qw52a9b11h2izr6ms6vbifqc7w7-builder.pl.drv'...
24building '/nix/store/ms1xzcqrf37174q21l3jbwjz74m3fgib-sowon-build.drv'...
25created 3 symlinks in user environment
26copying path '/nix/store/cc0lmsl3b94xj5wcvm3h34qdcy8z2kzc-gcc-8.3.0' from 'https://cache.nixos.org'...
27copying path '/nix/store/l2abq8hpgdjc4x7dwdps7zqcnxmjmjp4-gcc-wrapper-8.3.0' from 'https://cache.nixos.org'...
28copying path '/nix/store/90v0fyf5flnd5xc4fysx4q4c9y21gpf1-stdenv-linux' from 'https://cache.nixos.org'...

Build it using its Makefile, as follows:

1[nix-shell:~/src/C/tsoding/sowon]$ make
2gcc `pkg-config --cflags sdl2` -Wall -Wextra -std=c99 -pedantic -o sowon main.c `pkg-config --libs sdl2` -lm

Hakyll

To use an older Hakyll put the following in a shell.nix file:

 1{ nixpkgs ? import <nixpkgs> {}, compiler ? "default", doBenchmark ? false }:
 2
 3let
 4
 5  # TODO Hakyll is marked as broken in NixOS 20.09 so use 20.03's.
 6  # inherit (nixpkgs) pkgs;
 7  pkgs = import (builtins.fetchGit {
 8    # Descriptive name to make the store path easier to identify
 9    name = "my-old-revision";
10    url = "https://github.com/nixos/nixpkgs-channels/";
11    ref = "refs/heads/nixpkgs-20.03-darwin";
12    rev = "1975b8687474764c157e6a220fdcad2c5dc348a1";
13  }) {};
14
15  myPkg = pkgs.haskellPackages.hakyll;
16
17  f = { mkDerivation, base, hakyll, pandoc, stdenv }:
18      mkDerivation {
19        pname = "blog";
20        version = "0.1.0.0";
21        src = ./.;
22        isLibrary = false;
23        isExecutable = true;
24        executableHaskellDepends = [ base hakyll pandoc ];
25        license = "unknown";
26        hydraPlatforms = stdenv.lib.platforms.none;
27      };
28
29  haskellPackages = if compiler == "default"
30                       then pkgs.haskellPackages
31                       else pkgs.haskell.packages.${compiler};
32
33  variant = if doBenchmark then pkgs.haskell.lib.doBenchmark else pkgs.lib.id;
34
35  drv = variant (haskellPackages.callPackage f {});
36
37in
38
39  if pkgs.lib.inNixShell then drv.env else drv

And run the nix-shell command to get a shell with the Hakyll from NixOS 20.03.

Already on GitHub

Posts in this Series