THE PHOTON SPHERE

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:

with import <nixpkgs> {};

let
    stdenv8 = overrideCC stdenv gcc8;
in
    stdenv8.mkDerivation rec {
        name = "sowon-build";
        env = buildEnv {
            name = name;
            paths = buildInputs;
        };
        buildInputs = [
            pkgconfig
            SDL2
        ];
    }

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

$ nix-shell
these derivations will be built:
  /nix/store/z3cd8qw52a9b11h2izr6ms6vbifqc7w7-builder.pl.drv
  /nix/store/ms1xzcqrf37174q21l3jbwjz74m3fgib-sowon-build.drv
these paths will be fetched (34.44 MiB download, 136.10 MiB unpacked):
  /nix/store/71n1xcigc00w3z7yc836jqcx9cb2dys8-patchelf-0.9
  /nix/store/8bjj5zffqd7abvflgif7nl4vikmdp0jr-isl-0.17.1
  /nix/store/90v0fyf5flnd5xc4fysx4q4c9y21gpf1-stdenv-linux
  /nix/store/a9i1fi1if10pdvwmr44c2nwkd3lzy191-expand-response-params
  /nix/store/cc0lmsl3b94xj5wcvm3h34qdcy8z2kzc-gcc-8.3.0
  /nix/store/ckp85diipcr67cpmdablnih9fnrbjyyw-libmpc-1.1.0
  /nix/store/d43v6bx7r6fcaq3fbbfd5mwh1f5s7rmg-bash-interactive-4.4-p23-dev
  /nix/store/h53vw6vl9xq19x5y4prlwlfbpvnzh46s-gcc-8.3.0-lib
  /nix/store/l2abq8hpgdjc4x7dwdps7zqcnxmjmjp4-gcc-wrapper-8.3.0
  /nix/store/n4p9yimjanbkxiwlx3z0csacjlavb9sn-stdenv-linux
copying path '/nix/store/d43v6bx7r6fcaq3fbbfd5mwh1f5s7rmg-bash-interactive-4.4-p23-dev' from 'https://cache.nixos.org'...
copying path '/nix/store/a9i1fi1if10pdvwmr44c2nwkd3lzy191-expand-response-params' from 'https://cache.nixos.org'...
copying path '/nix/store/h53vw6vl9xq19x5y4prlwlfbpvnzh46s-gcc-8.3.0-lib' from 'https://cache.nixos.org'...
copying path '/nix/store/71n1xcigc00w3z7yc836jqcx9cb2dys8-patchelf-0.9' from 'https://cache.nixos.org'...
copying path '/nix/store/8bjj5zffqd7abvflgif7nl4vikmdp0jr-isl-0.17.1' from 'https://cache.nixos.org'...
copying path '/nix/store/ckp85diipcr67cpmdablnih9fnrbjyyw-libmpc-1.1.0' from 'https://cache.nixos.org'...
copying path '/nix/store/n4p9yimjanbkxiwlx3z0csacjlavb9sn-stdenv-linux' from 'https://cache.nixos.org'...
building '/nix/store/z3cd8qw52a9b11h2izr6ms6vbifqc7w7-builder.pl.drv'...
building '/nix/store/ms1xzcqrf37174q21l3jbwjz74m3fgib-sowon-build.drv'...
created 3 symlinks in user environment
copying path '/nix/store/cc0lmsl3b94xj5wcvm3h34qdcy8z2kzc-gcc-8.3.0' from 'https://cache.nixos.org'...
copying path '/nix/store/l2abq8hpgdjc4x7dwdps7zqcnxmjmjp4-gcc-wrapper-8.3.0' from 'https://cache.nixos.org'...
copying path '/nix/store/90v0fyf5flnd5xc4fysx4q4c9y21gpf1-stdenv-linux' from 'https://cache.nixos.org'...

Build it using its Makefile, as follows:

[nix-shell:~/src/C/tsoding/sowon]$ make
gcc `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:

{ nixpkgs ? import <nixpkgs> {}, compiler ? "default", doBenchmark ? false }:

let

  # TODO Hakyll is marked as broken in NixOS 20.09 so use 20.03's.
  # inherit (nixpkgs) pkgs;
  pkgs = import (builtins.fetchGit {
    # Descriptive name to make the store path easier to identify
    name = "my-old-revision";
    url = "https://github.com/nixos/nixpkgs-channels/";
    ref = "refs/heads/nixpkgs-20.03-darwin";
    rev = "1975b8687474764c157e6a220fdcad2c5dc348a1";
  }) {};

  myPkg = pkgs.haskellPackages.hakyll;

  f = { mkDerivation, base, hakyll, pandoc, stdenv }:
      mkDerivation {
        pname = "blog";
        version = "0.1.0.0";
        src = ./.;
        isLibrary = false;
        isExecutable = true;
        executableHaskellDepends = [ base hakyll pandoc ];
        license = "unknown";
        hydraPlatforms = stdenv.lib.platforms.none;
      };

  haskellPackages = if compiler == "default"
                       then pkgs.haskellPackages
                       else pkgs.haskell.packages.${compiler};

  variant = if doBenchmark then pkgs.haskell.lib.doBenchmark else pkgs.lib.id;

  drv = variant (haskellPackages.callPackage f {});

in

  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