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:
import <nixpkgs> {};
with
let
= overrideCC stdenv gcc8;
stdenv8 in
.mkDerivation rec {
stdenv8= "sowon-build";
name = buildEnv {
env = name;
name = buildInputs;
paths
};= [
buildInputs
pkgconfigSDL2
]; }
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:
? import <nixpkgs> {}, compiler ? "default", doBenchmark ? false }:
{ nixpkgs
let
# TODO Hakyll is marked as broken in NixOS 20.09 so use 20.03's.
# inherit (nixpkgs) pkgs;
= import (builtins.fetchGit {
pkgs # Descriptive name to make the store path easier to identify
= "my-old-revision";
name = "https://github.com/nixos/nixpkgs-channels/";
url = "refs/heads/nixpkgs-20.03-darwin";
ref = "1975b8687474764c157e6a220fdcad2c5dc348a1";
rev
}) {};
= pkgs.haskellPackages.hakyll;
myPkg
= { mkDerivation, base, hakyll, pandoc, stdenv }:
f
mkDerivation {= "blog";
pname = "0.1.0.0";
version = ./.;
src = false;
isLibrary = true;
isExecutable = [ base hakyll pandoc ];
executableHaskellDepends = "unknown";
license = stdenv.lib.platforms.none;
hydraPlatforms
};
= if compiler == "default"
haskellPackages then pkgs.haskellPackages
else pkgs.haskell.packages.${compiler};
= if doBenchmark then pkgs.haskell.lib.doBenchmark else pkgs.lib.id;
variant
= variant (haskellPackages.callPackage f {});
drv
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.