aboutsummaryrefslogtreecommitdiff
path: root/default.nix
blob: a5d6f4e98ed2ad30af6f9d810a57cc01839ba1f7 (about) (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
let
  pkgs = import <nixpkgs> { };
  rootSrc = pkgs.nix-gitignore.gitignoreSource [] ./.;
  nixfmt = import (builtins.fetchTarball
  "https://github.com/serokell/nixfmt/archive/master.tar.gz") { };

  # Taken from:
  # http://www.cs.yale.edu/homes/lucas.paul/posts/2017-04-10-hakyll-on-nix.html
  websiteBuilder = pkgs.stdenv.mkDerivation {
    name = "website-builder";
    src = ./hakyll;
    phases = "unpackPhase buildPhase";
    buildInputs =
    [(pkgs.haskellPackages.ghcWithPackages (p: with p; [hakyll]))];
    buildPhase = ''
      mkdir -p $out/bin
      ghc -O2 -dynamic --make Main.hs -o $out/bin/build-site
    '';
  };
  baseTask = pkgs.stdenv.mkDerivation {
    name = "website-task";
    src = rootSrc;
    buildInputs = [pkgs.perl];
    phases = "unpackPhase buildPhase";
    buildPhase = ''
      echo "ERROR: base task buildPhase not overriden."
      exit 1
    '';
  };
in with pkgs;
with pkgs.stdenv; rec {
  subtasks = rec {
    fixme = baseTask.overrideAttrs (baseAttrs: {
      name = "${baseAttrs.name}-fixme";
      buildInputs = baseAttrs.buildInputs ++ [ag];
      buildPhase = ''
        ag FIXME --ignore default.nix --ignore pastebin/skeleton.org || {
          touch $out
        }
      '';
    });
    perlPodCheck = baseTask.overrideAttrs (baseAttrs: {
      name = "${baseAttrs.name}-perl-podcheck";
      buildPhase = ''
        podchecker website pastebin/website-pastebin slides/website-slides
        touch $out
      '';
    });
    batsTest = baseTask.overrideAttrs (baseAttrs: {
      name = "${baseAttrs.name}-bats-test";
      buildInputs = baseAttrs.buildInputs ++ [bats];
      buildPhase = ''
        patchShebangs .
        ./t/website.bats
        touch $out
      '';
    });
    perlInternalTest = baseTask.overrideAttrs (baseAttrs: {
      name = "${baseAttrs.name}-perl-test";
      buildPhase = ''
        patchShebangs .
        ./website test
        touch $out
      '';
    });
    formatNix = baseTask.overrideAttrs (baseAttrs: {
      name = "${baseAttrs.name}-nixfmt";
      buildInputs = baseAttrs.buildInputs ++ [nixfmt];
      buildPhase = ''
        diff <(nixfmt < default.nix) default.nix || {
          echo "The default.nix is unformatted. To fix it, run:"
          echo "  nixfmt default.nix"
          exit 1
        }
        touch $out
      '';
    });
    hunspellCheck = baseTask.overrideAttrs (baseAttrs: {
      name = "${baseAttrs.name}-hunspell";
      buildInputs = baseAttrs.buildInputs
      ++ [(hunspellWithDicts (with hunspellDicts; [en-us]))];
      buildPhase = ''
        DICT="spelling/en_US-dict.txt"
        diff <(sort --ignore-case $DICT) $DICT || {
          echo "The $DICT dictionary is unsorted. To fix it, run:"
          echo "  sort $DICT | sort --ignore-case | sponge $DICT"
          exit 1
        }

        touch $out
      '';
    });
    docs = baseTask.overrideAttrs (baseAttrs: {
      name = "${baseAttrs.name}-docs";
      buildInputs = [ websiteBuilder pandoc ];
      buildPhase = ''
        export LOCALE_ARCHIVE="${pkgs.glibcLocales}/lib/locale/locale-archive";
        export LANG=en_US.UTF-8

        ls pastebin/*.org | awk -F. '{print $1}' | xargs -I{} pandoc -o {}.html {}.org --standalone

        build-site build

        mkdir $out
        cp -r _site/* $out
      '';
    });
  };
  test = baseTask.overrideAttrs (baseAttrs: rec {
    name = "${baseAttrs.name}-test";
    buildInputs = [
      subtasks.fixme
      subtasks.perlPodCheck
      subtasks.batsTest
      subtasks.perlInternalTest
      subtasks.formatNix
      subtasks.hunspellCheck
    ];
    buildPhase = ''
      echo "Ran tests for:"
      for d in ${builtins.toString buildInputs}; do
        echo "  $d"
      done
      echo "All tests passed!"
      touch $out
    '';
  });
  shell = mkShell rec {
    name = "website-shell";
    buildInputs = [
      websiteBuilder
      (pkgs.haskellPackages.ghcWithPackages (p: with p; [hakyll]))
      nixfmt
      (hunspellWithDicts (with hunspellDicts; [en-us]))
    ];
  };
  publishScript = pkgs.writeShellScriptBin "publish.sh" ''
    set -euo pipefail
    ${pkgs.rsync}/bin/rsync --verbose                               \
                            --copy-links                            \
                            --progress                              \
                            --stats                                 \
                            --update                                \
                            --recursive                             \
                            --rsh="ssh -o StrictHostKeyChecking=no" \
                            ${subtasks.docs}/                       \
                            "$SERVER_URL:$DOCS_SERVER_PATH"
  '';
}