]> Untitled Git - sneer-archive-site.git/blob - flake.nix
On branch main
[sneer-archive-site.git] / flake.nix
1 {
2   description = "A static site hosting the r/SneerClub archive";
3
4   inputs = {
5     flake-utils.url = "github:numtide/flake-utils";
6     archive-data.url = "git://these.awful.systems/sneer-archive-data.git";
7   };
8
9   outputs = { self, nixpkgs, flake-utils, archive-data }:
10     flake-utils.lib.eachDefaultSystem (system:
11       let pkgs = nixpkgs.legacyPackages."${system}";
12       in {
13         lib = pkgs.callPackage ./template.nix { };
14
15         packages.site = let
16           threads = (pkgs.lib.trivial.importJSON
17             "${archive-data.packages."${system}".default}/threads-newest.json");
18           bestest = (pkgs.lib.trivial.importJSON "${
19               archive-data.packages."${system}".default
20             }/submissions-bestest.json");
21           longest = (pkgs.lib.trivial.importJSON "${
22               archive-data.packages."${system}".default
23             }/submissions-longest.json");
24           newestPageFn = page: {
25             prev = if page - 1 == 0 then
26               "/archives"
27             else
28               "/archives/pages/${builtins.toString page}";
29             next = "/archives/pages/${builtins.toString (page + 2)}";
30             jump = n:
31               if n == 0 then
32                 "/archives"
33               else
34                 "/archives/pages/${builtins.toString (n + 1)}";
35           };
36           bestestPageFn = page: {
37             prev = if page - 1 == 0 then
38               "/archives/bestest"
39             else
40               "/archives/bestest/${builtins.toString page}";
41             next = "/archives/bestest/${builtins.toString (page + 2)}";
42             jump = n:
43               if n == 0 then
44                 "/archives/bestest"
45               else
46                 "/archives/bestest/${builtins.toString (n + 1)}";
47           };
48           longestPageFn = page: {
49             prev = if page - 1 == 0 then
50               "/archives/longest"
51             else
52               "/archives/longest/${builtins.toString page}";
53             next = "/archives/longest/${builtins.toString (page + 2)}";
54             jump = n:
55               if n == 0 then
56                 "/archives/longest"
57               else
58                 "/archives/longest/${builtins.toString (n + 1)}";
59           };
60           siteName = "r/SneerClub archives";
61           numPerPage = 25;
62           templates =
63             self.lib."${system}".mkTemplates "sneer-archive" ./templates [
64               "base.html"
65               "thread.html"
66               "comment.html"
67               "submission-page.html"
68               "pagination.html"
69               "topbar.html"
70               "search.html"
71             ];
72           baseWithSort = title: sort: content:
73             templates "base.html" {
74               inherit title sort content;
75               topbarTpl = templates "topbar.html";
76             };
77           base = title: content: baseWithSort title null content;
78           paginationTpl = templates "pagination.html";
79           submissionTpl = templates "submission-page.html";
80           topbarTpl = templates "topbar.html";
81           newestPages = builtins.map (n: {
82             inherit n;
83             content = baseWithSort
84               "${siteName} — page ${builtins.toString (n + 1)}" "newest"
85               (submissionTpl {
86                 inherit threads numPerPage topbarTpl paginationTpl;
87                 title = "${siteName} — page ${builtins.toString (n + 1)}";
88                 page = n;
89                 pageFn = newestPageFn n;
90               });
91           }) (pkgs.lib.lists.range 1
92             (builtins.floor (builtins.length threads / numPerPage)));
93           bestestPages = builtins.map (n: {
94             inherit n;
95             content = baseWithSort
96               "${siteName} — page ${builtins.toString (n + 1)}" "bestest"
97               (submissionTpl {
98                 inherit numPerPage topbarTpl paginationTpl;
99                 threads = bestest;
100                 title = "${siteName} — page ${builtins.toString (n + 1)}";
101                 page = n;
102                 pageFn = bestestPageFn n;
103               });
104           }) (pkgs.lib.lists.range 1
105             (builtins.floor (builtins.length bestest / numPerPage)));
106           longestPages = builtins.map (n: {
107             inherit n;
108             content = baseWithSort
109               "${siteName} — page ${builtins.toString (n + 1)}" "longest"
110               (submissionTpl {
111                 inherit numPerPage topbarTpl paginationTpl;
112                 threads = longest;
113                 title = "${siteName} — page ${builtins.toString (n + 1)}";
114                 page = n;
115                 pageFn = longestPageFn n;
116               });
117           }) (pkgs.lib.lists.range 1
118             (builtins.floor (builtins.length longest / numPerPage)));
119           threadPages = builtins.map (thread: {
120             id = thread.id;
121             content = base "${thread.title} — ${siteName}"
122               (templates "thread.html" {
123                 inherit thread topbarTpl;
124                 commentTpl = templates "comment.html";
125               });
126           }) threads;
127           searchResults = base "Search results — ${siteName}"
128             (templates "search.html" { });
129         in pkgs.runCommand "generate-site" { } ''
130           mkdir -p $out/archives
131           mkdir -p $out/archives/thread
132           mkdir -p $out/archives/pages
133           mkdir -p $out/archives/bestest
134           mkdir -p $out/archives/longest
135
136           cat <<'__EOF__' > $out/archives/index.html
137           ${baseWithSort siteName "newest" (submissionTpl {
138             inherit threads numPerPage topbarTpl paginationTpl;
139             title = siteName;
140             page = 0;
141             pageFn = newestPageFn 0;
142           })}
143           __EOF__
144           ${builtins.concatStringsSep "\n" (builtins.map (page: ''
145             cat <<'__EOF__' > $out/archives/pages/${
146               builtins.toString (page.n + 1)
147             }.html
148             ${page.content}
149             __EOF__
150           '') newestPages)}
151
152           cat <<'__EOF__' > $out/archives//bestest.html
153           ${baseWithSort siteName "bestest" (submissionTpl {
154             inherit numPerPage topbarTpl paginationTpl;
155             title = siteName;
156             threads = bestest;
157             page = 0;
158             pageFn = bestestPageFn 0;
159           })}
160           __EOF__
161           ${builtins.concatStringsSep "\n" (builtins.map (page: ''
162             cat <<'__EOF__' > $out/archives/bestest/${
163               builtins.toString (page.n + 1)
164             }.html
165             ${page.content}
166             __EOF__
167           '') bestestPages)}
168
169           cat <<'__EOF__' > $out/archives/longest.html
170           ${baseWithSort siteName "longest" (submissionTpl {
171             inherit numPerPage topbarTpl paginationTpl;
172             title = siteName;
173             threads = longest;
174             page = 0;
175             pageFn = longestPageFn 0;
176           })}
177           __EOF__
178           ${builtins.concatStringsSep "\n" (builtins.map (page: ''
179             cat <<'__EOF__' > $out/archives/longest/${
180               builtins.toString (page.n + 1)
181             }.html
182             ${page.content}
183             __EOF__
184           '') longestPages)}
185
186           ${builtins.concatStringsSep "\n" (builtins.map (page: ''
187             cat <<'__EOF__' > $out/archives/thread/${page.id}.html
188             ${page.content}
189             __EOF__
190           '') threadPages)}
191
192           cat <<'__EOF__' > $out/archives/search.html
193           ${searchResults}
194           __EOF__
195
196           cp ${
197             archive-data.packages."${system}".default
198           }/submissions-bestest.json $out/archives/lunr.json
199
200           if [ -n "$(ls -A ${./static} 2>/dev/null)" ]; then cp -R ${
201             ./static
202           }/* $out/archives; fi
203         '';
204
205         packages.serve = let
206         in pkgs.writeShellScriptBin "serve" ''
207           shopt -s globstar
208           while true; do
209                 ls -d templates/* static/* **/*.nix flake.lock | ${pkgs.entr}/bin/entr -r -d bash -c "\
210                    /usr/bin/env time -f 'Nix rebuild finished in %es'\
211                    nix build $1 --show-trace && \
212                    ${pkgs.caddy}/bin/caddy run --config ${
213                      ./Caddyfile
214                    } --adapter caddyfile"
215                 if [ $? -eq 0 ]; then break; fi
216           done
217         '';
218
219         packages.default = self.packages."${system}".site;
220
221         devShells.default = pkgs.mkShell {
222           buildInputs = [
223             self.packages."${system}".serve
224             pkgs.html-tidy
225             archive-data.packages."${system}".default # cache archive data
226           ];
227         };
228       });
229 }