]> Git — Sourcephile - sourcephile-web.git/blob - flake.nix
init
[sourcephile-web.git] / flake.nix
1 {
2 description = "sourcephile.fr website";
3 inputs = {
4 nixpkgs.url = "flake:nixpkgs";
5 #ema.url = "path:/home/julm/src/haskell/ema/ema";
6 ema.url = "github:srid/ema/89c9f9234c47437513774efa7e24ef0bdd6427bb";
7 ema.inputs.nixpkgs.follows = "nixpkgs";
8 ema.inputs.pre-commit-hooks.follows = "pre-commit-hooks";
9 ema.inputs.url-slug.follows = "url-slug";
10 url-slug.url = "github:srid/url-slug";
11 url-slug.inputs.nixpkgs.follows = "nixpkgs";
12 commonmark-simple.url = "github:srid/commonmark-simple";
13 commonmark-simple.inputs.nixpkgs.follows = "nixpkgs";
14 unionmount.url = "github:srid/unionmount";
15 unionmount.inputs.nixpkgs.follows = "nixpkgs";
16
17 pre-commit-hooks.inputs.nixpkgs.follows = "nixpkgs";
18 pre-commit-hooks.url = "github:cachix/pre-commit-hooks.nix";
19 PyF.url = "github:guibou/PyF/main";
20 PyF.inputs.nixpkgs.follows = "nixpkgs";
21 open-iconic.url = "github:iconic/open-iconic";
22 open-iconic.flake = false;
23 windiCSS = {
24 url = "github:srid/windicss-nix";
25 flake = false;
26 };
27 # .git/ is needed by the generator to use git log
28 selfGit.url = "path:/home/julm/work/sourcephile/haskell/sourcephile-web/.git";
29 selfGit.flake = false;
30
31 tailwind-haskell.url = "github:srid/tailwind-haskell/master";
32 tailwind-haskell.inputs.nixpkgs.follows = "nixpkgs";
33 tailwind-haskell.inputs.flake-utils.follows = "ema/flake-utils";
34 tailwind-haskell.inputs.flake-compat.follows = "ema/flake-compat";
35 };
36 outputs = inputs:
37 let
38 lib = inputs.nixpkgs.lib;
39 forAllSystems = f: lib.genAttrs lib.systems.flakeExposed (system: f rec {
40 inherit system;
41 pkgs = inputs.nixpkgs.legacyPackages.${system};
42 haskellPackages = pkgs.haskellPackages/*.packages.ghc901*/.extend (with pkgs.haskell.lib; hfinal: hprev: {
43 # ${pkg} = buildFromSdist (hprev.callCabal2nix pkg ./. {});
44 generator = hprev.callCabal2nix "generator" ./generator { };
45 ema = disableCabalFlag inputs.ema.defaultPackage.${system} "with-examples";
46 tailwind = inputs.tailwind-haskell.defaultPackage.${system};
47 url-slug = inputs.url-slug.defaultPackage.${system};
48 commonmark-simple = inputs.commonmark-simple.defaultPackage.${system};
49 unionmount = inputs.unionmount.defaultPackage.${system};
50 PyF = inputs.PyF.defaultPackage.${system};
51 #thumbnail-plus = doJailbreak (unmarkBroken hprev.thumbnail-plus);
52 thumbnail-plus = buildFromSdist (hprev.callCabal2nix "thumbnail-plus"
53 (pkgs.fetchFromGitHub {
54 owner = "abhin4v";
55 repo = "thumbnail-plus";
56 rev = "fc10600d3af19ccce7c69416e2ec33143faa4d37";
57 sha256 = "sha256-lhkhd7pSDq1AhF6jAnWl0at7s4RQvuW+nDjboA3W0D4=";
58 })
59 { });
60 });
61 windiCSS = (import inputs.windiCSS { inherit pkgs; }).shell.nodeDependencies;
62 tailwindCSS = pkgs.fetchurl {
63 url = "https://unpkg.com/tailwindcss@2/dist/tailwind.css";
64 sha256 = "sha256-3jBdkj3eH4VZ53NLsfAZcK86du/IRLlMo8hIj+qNo/c=";
65 #url = "https://unpkg.com/tailwindcss@2/dist/tailwind.min.css";
66 #sha256 = "sha256-tq2XQC7duQPnpdenPuR6Z5IE773aRSGjkcutnfUJuTI=";
67 };
68 windiCSS-extras = pkgs.runCommand "windi-extras.css" { } ''
69 ${windiCSS}/bin/windicss ${generator/css/windi-extras.html} -o $out
70 '';
71 watch-site = pkgs.writeShellScriptBin "watch-site" ''
72 set -eux
73 mkdir -p content/static/icons
74 # FIXME: unfortunately browsers may not invalidate their caching for those .css
75 ln -fns ${tailwindCSS} content/static/css/windi.css
76 ln -fns ${windiCSS-extras} content/static/css/windi-extras.css
77 ln -fns ${inputs.open-iconic} content/static/icons/open-iconic
78 exec ${pkgs.ghcid}/bin/ghcid --no-height-limit --reverse-errors -C generator --warnings \
79 -T "System.Directory.withCurrentDirectory \"../content\" Main.main"
80 '';
81 });
82 in
83 rec {
84 # nix -L build
85 defaultPackage = forAllSystems (args: with args; packages.${system}.website);
86 packages = forAllSystems (args: with args; rec {
87 inherit (haskellPackages) generator;
88 generated = pkgs.runCommand "generated"
89 {
90 LOCALE_ARCHIVE = "${pkgs.glibcLocales}/lib/locale/locale-archive";
91 LC_ALL = "fr_FR.UTF-8";
92 } ''
93 set -eux
94 PATH="${lib.makeBinPath [pkgs.git]}:$PATH"
95 echo >.git "gitdir: ${inputs.selfGit}"
96 cp -r --no-preserve=mode -s ${./content}/* .
97 ln -fns ${windiCSS-extras} static/css/windi-extras.css
98 rm -rf static/icons/open-iconic
99 echo >lastModified ${toString inputs.self.lastModified}
100 mkdir $out
101 ${generator}/bin/generator gen $out
102 mkdir -p $out/static/icons
103 ln -s ${inputs.open-iconic} $out/static/icons/open-iconic
104 '';
105 # Produce a CSS file that contains only the WindiCSS styles
106 # in use by the generated HTML files.
107 windicss = pkgs.runCommand "windi.css" { } ''
108 ${windiCSS}/bin/windicss '${generated}/**/*.html' -o $out --preflight --minify
109 '';
110 website = pkgs.runCommand "website" { } ''
111 set -eux
112 # Copy because HTML files will be modified
113 cp -r --no-preserve=mode ${generated} $out
114 # Name with a hash to invalidate browser's cache if the CSS has changed
115 cssHash=$(md5sum ${windicss} | cut -f 1 -d ' ')
116 mkdir -p $out/static/css
117 ln -s ${windicss} $out/static/css/windi.$cssHash.css
118 # Retroactively replace the CSS with the minified CSS
119 find $out -iname "*.html" -exec \
120 sed -i "s|/static/css/windi.css|/static/css/windi.$cssHash.css|" {} +
121 '';
122 });
123 # direnv allow
124 devShell = forAllSystems (args: with args; haskellPackages.shellFor {
125 packages = ps: [ ps.generator ];
126 nativeBuildInputs = with haskellPackages; [
127 cabal-fmt
128 cabal-install
129 fourmolu
130 ghcid
131 haskell-language-server
132 pkgs.git-chglog
133 pkgs.reuse
134 windiCSS
135 watch-site
136 ];
137 inherit (checks.${system}.pre-commit-check) shellHook;
138 #withHoogle = true;
139 });
140 # nix flake check
141 checks = forAllSystems (args: with args; {
142 pre-commit-check = inputs.pre-commit-hooks.lib.${system}.run {
143 src = ./.;
144 settings.ormolu.defaultExtensions = [
145 "ImportQualifiedPost"
146 "TypeApplications"
147 ];
148 hooks = {
149 hlint.enable = true;
150 nixpkgs-fmt.enable = true;
151 fourmolu.enable = true;
152 cabal-fmt.enable = true;
153 };
154 };
155 });
156 # nix run .#watch
157 defaultApp = apps.watch;
158 apps = forAllSystems (args: with args; {
159 watch = {
160 type = "app";
161 program = watch-site + "/bin/watch-site";
162 };
163 hoogle = {
164 type = "app";
165 program = (pkgs.writeShellScript "hoogle-server" ''exec hoogle server --local --links -p 8080'').outPath;
166 };
167 });
168 };
169 }