Skip to content

Commit bca9cf5

Browse files
authored
Switch to using Nix flakes (#253)
Someone has requested me to build this book, since they didn't have Nix. So when looking at the Nix expressions I found that there was only a shell.nix and the default.nix was essentially just aliasing the shell.nix but without the pinning. However, with Nix flakes we no longer need to do pinning like this and we instead have a flake.lock file, which essentially pins the corresponding revisions. I also added a default.nix, which uses flake-compat to make sure that nix-shell and nix-build work as before. Since so far the way to build the PDF(s) was to get into a Nix shell and run make accordingly. For me however it's unacceptable to build random code without the Nix sandbox, so while writing a proper default.nix I decided it would be better to turn it into a Nix flake. Apart from the previous ways to build this project we now have: $ nix develop # To get a Nix shell (similar to nix-shell but cached) $ nix build # The default edition $ nix build .\#ctfp-scala # The Scala edition $ nix build .\#ctfp-ocaml # The OCaml edition Signed-off-by: aszlig <aszlig@nix.build>
1 parent 39d625f commit bca9cf5

File tree

6 files changed

+190
-121
lines changed

6 files changed

+190
-121
lines changed

default.nix

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
# nix-shell --pure --command 'cd src; make'
21
let
3-
nixpkgs = import <nixpkgs> {};
4-
in
5-
nixpkgs.callPackage ./shell.nix {}
2+
rev = "cecfd08d13ddef8a79f277e67b8084bd9afa1586";
3+
url = "https://github.com/edolstra/flake-compat/archive/${rev}.tar.gz";
4+
flake = import (fetchTarball url) { src = ./.; };
5+
inNixShell = builtins.getEnv "IN_NIX_SHELL" != "";
6+
in if inNixShell then flake.shellNix else flake.defaultNix

flake.lock

Lines changed: 42 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

flake.nix

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
{
2+
description = "Category Theory for Programmers";
3+
4+
inputs.nixpkgs.url = "nixpkgs/nixos-20.03";
5+
inputs.utils.url = "github:numtide/flake-utils";
6+
7+
outputs = { self, nixpkgs, utils }: utils.lib.eachDefaultSystem (system: let
8+
pkgs = nixpkgs.legacyPackages.${system};
9+
inherit (nixpkgs) lib;
10+
11+
###########################################################################
12+
# LaTeX Environment
13+
texliveEnv = pkgs.texlive.combine {
14+
inherit (pkgs.texlive)
15+
bookcover
16+
textpos
17+
fgruler
18+
tcolorbox
19+
fvextra
20+
framed
21+
newtx
22+
nowidow
23+
emptypage
24+
wrapfig
25+
subfigure
26+
adjustbox
27+
collectbox
28+
tikz-cd
29+
imakeidx
30+
idxlayout
31+
titlesec
32+
subfiles
33+
lettrine
34+
upquote
35+
libertine
36+
mweights
37+
fontaxes
38+
mdframed
39+
needspace
40+
xifthen
41+
ifnextok
42+
currfile
43+
noindentafter
44+
ifmtarg
45+
scheme-medium
46+
listings
47+
minted
48+
microtype
49+
babel
50+
todonotes
51+
chngcntr
52+
ifplatform
53+
xstring
54+
minifp
55+
titlecaps
56+
enumitem
57+
environ
58+
trimspaces
59+
l3packages
60+
zref
61+
catchfile
62+
import
63+
;
64+
};
65+
66+
###########################################################################
67+
# Python Environment
68+
69+
# Pin the Python version and its associated package set in a single place.
70+
python = pkgs.python38;
71+
pythonPkgs = pkgs.python38Packages;
72+
73+
pygments-style-github = pythonPkgs.buildPythonPackage rec {
74+
pname = "pygments-style-github";
75+
version = "0.4";
76+
77+
doCheck = false; # Hopefully someone else has run the tests.
78+
79+
src = pythonPkgs.fetchPypi {
80+
inherit pname version;
81+
sha256 = "19zl8l5fyb8z3j8pdlm0zbgag669af66f8gn81ichm3x2hivvjhg";
82+
};
83+
84+
# Anything depending on this derivation is probably also gonna want
85+
# pygments to be available.
86+
propagatedBuildInputs = with pythonPkgs; [ pygments ];
87+
};
88+
89+
pythonEnv = python.withPackages (
90+
pyPkgs: with pyPkgs; [
91+
pygments
92+
pygments-style-github
93+
]
94+
);
95+
96+
mkPackageName = edition:
97+
"ctfp${lib.optionalString (edition != null) "-${edition}"}";
98+
99+
mkPackage = isShell: edition: pkgs.stdenv.mkDerivation {
100+
name = mkPackageName edition;
101+
src = if isShell then null else self;
102+
103+
makeFlags = [
104+
"-C" "src" "OUTPUT_DIR=$(out)"
105+
"GIT_VER=${self.rev or self.lastModifiedDate}"
106+
];
107+
108+
buildFlags = lib.optional (edition != null) edition;
109+
110+
dontInstall = true;
111+
112+
FONTCONFIG_FILE = pkgs.makeFontsConf {
113+
fontDirectories = with pkgs; [ inconsolata-lgc libertine libertinus ];
114+
};
115+
116+
buildInputs = with pkgs; [
117+
# Misc. build tooling.
118+
gnumake
119+
git
120+
python3Packages.virtualenv
121+
which
122+
# LaTeX Environment (with all associated libraries and packages).
123+
texliveEnv
124+
# Python Environment (with all associated libraries and packages).
125+
pythonEnv
126+
];
127+
};
128+
129+
editions = [ null "scala" "ocaml" ];
130+
131+
in {
132+
packages = lib.listToAttrs (map (edition: {
133+
name = mkPackageName edition;
134+
value = mkPackage false edition;
135+
}) editions);
136+
defaultPackage = self.packages.${system}.ctfp;
137+
devShell = mkPackage true null;
138+
});
139+
}

pinned.nix

Lines changed: 0 additions & 8 deletions
This file was deleted.

shell.nix

Lines changed: 0 additions & 106 deletions
This file was deleted.

src/Makefile

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
DIR := $(shell pwd)
77
GIT_VER := $(shell git describe --tags --always --long | tr -d '\n')
8+
OUTPUT_DIR := ../out/$(GIT_VER)
89

910
OUTPUT = category-theory-for-programmers
1011

@@ -54,7 +55,7 @@ $(TOPPDFFILES) : %.pdf : %.tex $(TEXFILES)
5455
if which latexmk > /dev/null 2>&1 ;\
5556
then \
5657
latexmk -shell-escape -interaction=nonstopmode -halt-on-error -norc -jobname=ctfp -pdflatex="xelatex %O %S" -pdf $< ;\
57-
mv ctfp.pdf ../out/$(GIT_VER)/$(subst ctfp,$(OUTPUT),$(subst ctfp-reader,$(OUTPUT),$*)).pdf ;\
58+
mv ctfp.pdf $(OUTPUT_DIR)/$(subst ctfp,$(OUTPUT),$(subst ctfp-reader,$(OUTPUT),$*)).pdf ;\
5859
else @printf "Error: unable to find latexmk. Is it installed?\n" ;\
5960
fi
6061

@@ -64,8 +65,8 @@ version.tex:
6465
@printf '}' >> version.tex
6566

6667
out-dir:
67-
@printf 'Creating output directory: $(GIT_VER)\n'
68-
mkdir -p ../out/$(GIT_VER)
68+
@printf 'Creating output directory: $(OUTPUT_DIR)\n'
69+
mkdir -p $(OUTPUT_DIR)
6970

7071
clean:
7172
rm -f *~ *.aux {ctfp-*}.{out,log,pdf,dvi,fls,fdb_latexmk,aux,brf,bbl,idx,ilg,ind,toc,sed}

0 commit comments

Comments
 (0)