From bca9cf5d3d5b5e368260cc824af1b8dc822d778a Mon Sep 17 00:00:00 2001 From: aszlig Date: Fri, 4 Sep 2020 14:16:42 +0200 Subject: [PATCH] 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 --- default.nix | 9 ++-- flake.lock | 42 ++++++++++++++++ flake.nix | 139 +++++++++++++++++++++++++++++++++++++++++++++++++++ pinned.nix | 8 --- shell.nix | 106 --------------------------------------- src/Makefile | 7 +-- 6 files changed, 190 insertions(+), 121 deletions(-) create mode 100644 flake.lock create mode 100644 flake.nix delete mode 100644 pinned.nix delete mode 100644 shell.nix diff --git a/default.nix b/default.nix index a0619731..eae45ac8 100644 --- a/default.nix +++ b/default.nix @@ -1,5 +1,6 @@ -# nix-shell --pure --command 'cd src; make' let - nixpkgs = import {}; -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 diff --git a/flake.lock b/flake.lock new file mode 100644 index 00000000..c53881bd --- /dev/null +++ b/flake.lock @@ -0,0 +1,42 @@ +{ + "nodes": { + "nixpkgs": { + "locked": { + "lastModified": 1599158497, + "narHash": "sha256-lHTfnlSK+hm2yvGuKvGe6+Wc+TJf/h6SNLd0u+K+DdE=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "fa3fb57d322a7f1bc93f763b10961144b37b9633", + "type": "github" + }, + "original": { + "id": "nixpkgs", + "ref": "nixos-20.03", + "type": "indirect" + } + }, + "root": { + "inputs": { + "nixpkgs": "nixpkgs", + "utils": "utils" + } + }, + "utils": { + "locked": { + "lastModified": 1598466101, + "narHash": "sha256-JPhv+Ay98KMWRVRFlLEK9+eLpvNjhTBGWdFKZsE97ck=", + "owner": "numtide", + "repo": "flake-utils", + "rev": "a586a6b966d59f53f45a04f8891fbc017dc09dbe", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "flake-utils", + "type": "github" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/flake.nix b/flake.nix new file mode 100644 index 00000000..a32f2508 --- /dev/null +++ b/flake.nix @@ -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; + }); +} diff --git a/pinned.nix b/pinned.nix deleted file mode 100644 index 042d96a9..00000000 --- a/pinned.nix +++ /dev/null @@ -1,8 +0,0 @@ -# This commit corresponds to a recent (as of 2 August 2020) pin of the 20.03 -# nixpkgs branch. -import ( - builtins.fetchTarball { - url = "https://github.com/nixos/nixpkgs/archive/7dc4385dc7b5b2c0dbfecd774cebbc87ac05c061.tar.gz"; - sha256 = "10xr87ilxypz8fya6q42vsk5rmavv1bjrxhkznr9fvn514am58xb"; - } -) diff --git a/shell.nix b/shell.nix deleted file mode 100644 index d952f1db..00000000 --- a/shell.nix +++ /dev/null @@ -1,106 +0,0 @@ -{ pkgs ? import ./pinned.nix {} }: - -let - ############################################################################# - # 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 - ] - ); -in - -pkgs.mkShell { - 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 - ]; -} diff --git a/src/Makefile b/src/Makefile index 2b236714..3c6c2da4 100644 --- a/src/Makefile +++ b/src/Makefile @@ -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 @@ -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 @@ -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}