Skip to content

Commit

Permalink
Switch to using Nix flakes (#253)
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
aszlig authored Sep 4, 2020
1 parent 39d625f commit bca9cf5
Show file tree
Hide file tree
Showing 6 changed files with 190 additions and 121 deletions.
9 changes: 5 additions & 4 deletions default.nix
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# nix-shell --pure --command 'cd src; make'
let
nixpkgs = import <nixpkgs> {};
in
nixpkgs.callPackage ./shell.nix {}
rev = "cecfd08d13ddef8a79f277e67b8084bd9afa1586";
url = "https://github.com/edolstra/flake-compat/archive/${rev}.tar.gz";
flake = import (fetchTarball url) { src = ./.; };
inNixShell = builtins.getEnv "IN_NIX_SHELL" != "";
in if inNixShell then flake.shellNix else flake.defaultNix
42 changes: 42 additions & 0 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

139 changes: 139 additions & 0 deletions flake.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
{
description = "Category Theory for Programmers";

inputs.nixpkgs.url = "nixpkgs/nixos-20.03";
inputs.utils.url = "github:numtide/flake-utils";

outputs = { self, nixpkgs, utils }: utils.lib.eachDefaultSystem (system: let
pkgs = nixpkgs.legacyPackages.${system};
inherit (nixpkgs) lib;

###########################################################################
# LaTeX Environment
texliveEnv = pkgs.texlive.combine {
inherit (pkgs.texlive)
bookcover
textpos
fgruler
tcolorbox
fvextra
framed
newtx
nowidow
emptypage
wrapfig
subfigure
adjustbox
collectbox
tikz-cd
imakeidx
idxlayout
titlesec
subfiles
lettrine
upquote
libertine
mweights
fontaxes
mdframed
needspace
xifthen
ifnextok
currfile
noindentafter
ifmtarg
scheme-medium
listings
minted
microtype
babel
todonotes
chngcntr
ifplatform
xstring
minifp
titlecaps
enumitem
environ
trimspaces
l3packages
zref
catchfile
import
;
};

###########################################################################
# Python Environment

# Pin the Python version and its associated package set in a single place.
python = pkgs.python38;
pythonPkgs = pkgs.python38Packages;

pygments-style-github = pythonPkgs.buildPythonPackage rec {
pname = "pygments-style-github";
version = "0.4";

doCheck = false; # Hopefully someone else has run the tests.

src = pythonPkgs.fetchPypi {
inherit pname version;
sha256 = "19zl8l5fyb8z3j8pdlm0zbgag669af66f8gn81ichm3x2hivvjhg";
};

# Anything depending on this derivation is probably also gonna want
# pygments to be available.
propagatedBuildInputs = with pythonPkgs; [ pygments ];
};

pythonEnv = python.withPackages (
pyPkgs: with pyPkgs; [
pygments
pygments-style-github
]
);

mkPackageName = edition:
"ctfp${lib.optionalString (edition != null) "-${edition}"}";

mkPackage = isShell: edition: pkgs.stdenv.mkDerivation {
name = mkPackageName edition;
src = if isShell then null else self;

makeFlags = [
"-C" "src" "OUTPUT_DIR=$(out)"
"GIT_VER=${self.rev or self.lastModifiedDate}"
];

buildFlags = lib.optional (edition != null) edition;

dontInstall = true;

FONTCONFIG_FILE = pkgs.makeFontsConf {
fontDirectories = with pkgs; [ inconsolata-lgc libertine libertinus ];
};

buildInputs = with pkgs; [
# Misc. build tooling.
gnumake
git
python3Packages.virtualenv
which
# LaTeX Environment (with all associated libraries and packages).
texliveEnv
# Python Environment (with all associated libraries and packages).
pythonEnv
];
};

editions = [ null "scala" "ocaml" ];

in {
packages = lib.listToAttrs (map (edition: {
name = mkPackageName edition;
value = mkPackage false edition;
}) editions);
defaultPackage = self.packages.${system}.ctfp;
devShell = mkPackage true null;
});
}
8 changes: 0 additions & 8 deletions pinned.nix

This file was deleted.

106 changes: 0 additions & 106 deletions shell.nix

This file was deleted.

7 changes: 4 additions & 3 deletions src/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

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

OUTPUT = category-theory-for-programmers

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

Expand All @@ -64,8 +65,8 @@ version.tex:
@printf '}' >> version.tex

out-dir:
@printf 'Creating output directory: $(GIT_VER)\n'
mkdir -p ../out/$(GIT_VER)
@printf 'Creating output directory: $(OUTPUT_DIR)\n'
mkdir -p $(OUTPUT_DIR)

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

0 comments on commit bca9cf5

Please sign in to comment.