2 description = "A static site hosting the r/SneerClub archive";
5 flake-utils.url = "github:numtide/flake-utils";
6 archive-data.url = "git://these.awful.systems/sneer-archive-data.git";
9 outputs = { self, nixpkgs, flake-utils, archive-data }:
10 flake-utils.lib.eachDefaultSystem (system:
11 let pkgs = nixpkgs.legacyPackages."${system}";
13 lib = pkgs.callPackage ./template.nix { };
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
28 "/archives/pages/${builtins.toString page}";
29 next = "/archives/pages/${builtins.toString (page + 2)}";
34 "/archives/pages/${builtins.toString (n + 1)}";
36 bestestPageFn = page: {
37 prev = if page - 1 == 0 then
40 "/archives/bestest/${builtins.toString page}";
41 next = "/archives/bestest/${builtins.toString (page + 2)}";
46 "/archives/bestest/${builtins.toString (n + 1)}";
48 longestPageFn = page: {
49 prev = if page - 1 == 0 then
52 "/archives/longest/${builtins.toString page}";
53 next = "/archives/longest/${builtins.toString (page + 2)}";
58 "/archives/longest/${builtins.toString (n + 1)}";
60 siteName = "r/SneerClub archives";
63 self.lib."${system}".mkTemplates "sneer-archive" ./templates [
67 "submission-page.html"
72 baseWithSort = title: sort: content:
73 templates "base.html" {
74 inherit title sort content;
75 topbarTpl = templates "topbar.html";
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: {
83 content = baseWithSort
84 "${siteName} — page ${builtins.toString (n + 1)}" "newest"
86 inherit threads numPerPage topbarTpl paginationTpl;
87 title = "${siteName} — page ${builtins.toString (n + 1)}";
89 pageFn = newestPageFn n;
91 }) (pkgs.lib.lists.range 1
92 (builtins.floor (builtins.length threads / numPerPage)));
93 bestestPages = builtins.map (n: {
95 content = baseWithSort
96 "${siteName} — page ${builtins.toString (n + 1)}" "bestest"
98 inherit numPerPage topbarTpl paginationTpl;
100 title = "${siteName} — page ${builtins.toString (n + 1)}";
102 pageFn = bestestPageFn n;
104 }) (pkgs.lib.lists.range 1
105 (builtins.floor (builtins.length bestest / numPerPage)));
106 longestPages = builtins.map (n: {
108 content = baseWithSort
109 "${siteName} — page ${builtins.toString (n + 1)}" "longest"
111 inherit numPerPage topbarTpl paginationTpl;
113 title = "${siteName} — page ${builtins.toString (n + 1)}";
115 pageFn = longestPageFn n;
117 }) (pkgs.lib.lists.range 1
118 (builtins.floor (builtins.length longest / numPerPage)));
119 threadPages = builtins.map (thread: {
121 content = base "${thread.title} — ${siteName}"
122 (templates "thread.html" {
123 inherit thread topbarTpl;
124 commentTpl = templates "comment.html";
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
136 cat <<'__EOF__' > $out/archives/index.html
137 ${baseWithSort siteName "newest" (submissionTpl {
138 inherit threads numPerPage topbarTpl paginationTpl;
141 pageFn = newestPageFn 0;
144 ${builtins.concatStringsSep "\n" (builtins.map (page: ''
145 cat <<'__EOF__' > $out/archives/pages/${
146 builtins.toString (page.n + 1)
152 cat <<'__EOF__' > $out/archives//bestest.html
153 ${baseWithSort siteName "bestest" (submissionTpl {
154 inherit numPerPage topbarTpl paginationTpl;
158 pageFn = bestestPageFn 0;
161 ${builtins.concatStringsSep "\n" (builtins.map (page: ''
162 cat <<'__EOF__' > $out/archives/bestest/${
163 builtins.toString (page.n + 1)
169 cat <<'__EOF__' > $out/archives/longest.html
170 ${baseWithSort siteName "longest" (submissionTpl {
171 inherit numPerPage topbarTpl paginationTpl;
175 pageFn = longestPageFn 0;
178 ${builtins.concatStringsSep "\n" (builtins.map (page: ''
179 cat <<'__EOF__' > $out/archives/longest/${
180 builtins.toString (page.n + 1)
186 ${builtins.concatStringsSep "\n" (builtins.map (page: ''
187 cat <<'__EOF__' > $out/archives/thread/${page.id}.html
192 cat <<'__EOF__' > $out/archives/search.html
197 archive-data.packages."${system}".default
198 }/submissions-bestest.json $out/archives/lunr.json
200 if [ -n "$(ls -A ${./static} 2>/dev/null)" ]; then cp -R ${
202 }/* $out/archives; fi
206 in pkgs.writeShellScriptBin "serve" ''
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 ${
214 } --adapter caddyfile"
215 if [ $? -eq 0 ]; then break; fi
219 packages.default = self.packages."${system}".site;
221 devShells.default = pkgs.mkShell {
223 self.packages."${system}".serve
225 archive-data.packages."${system}".default # cache archive data