From e7615c4947728f71c196f8b08f75e4d52585ccee Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Fri, 30 Dec 2022 15:41:50 -0800 Subject: [PATCH] Generate static files as part of the build process --- build/builder.sh | 19 ++++++++++++++----- flake.lock | 28 ++++------------------------ flake.nix | 13 +++---------- lib.nix | 34 +++++++++------------------------- 4 files changed, 30 insertions(+), 64 deletions(-) diff --git a/build/builder.sh b/build/builder.sh index fb551e8..4d1dada 100644 --- a/build/builder.sh +++ b/build/builder.sh @@ -1,14 +1,23 @@ source $stdenv/setup +# Set up Ruby to use UTF-8 +export RUBYOPT="-KU -E utf-8:utf-8" + # Copy files to a mutable directory. cp -r $src/* . - -# Hugo can't set baseUrl via CLI for multi-lingual hosts. -# We have to manually edit the configuration. -patchPhase +# We'll generate some static files so make static writable +chmod -R u+w . # Build site with Hugo -hugo $extraFlags +hugo $hugoFlags --config=config.toml,config-gen.toml + +# Create generated files +# Can't do submodules because nix flake inputs get their .git deleted +ruby ./analyze.rb > static/graph/graph.gen.js # Graph files +stork build --input public/index.toml --output static/index.st # Search index + +# Static folder changed, re-run Hugo +hugo $hugoFlags --config=config.toml,config-gen.toml # Output result mkdir $out diff --git a/flake.lock b/flake.lock index aeeb8aa..4869898 100644 --- a/flake.lock +++ b/flake.lock @@ -3,11 +3,11 @@ "blog-source": { "flake": false, "locked": { - "lastModified": 1672436100, - "narHash": "sha256-shDvdyMXeTs2VVfIOOIQpF6eAe/5nAS8vZL2QA7ryIA=", + "lastModified": 1672443445, + "narHash": "sha256-s7woTyMkyrtudu20bUcbyF5HXFXGa4NdM7H9id4mTVw=", "ref": "master", - "rev": "e7cb818f055d91faabfe3eb4919b6508b29d28c6", - "revCount": 609, + "rev": "0dfb964e99d20a8d630dacc7670e77bcbea8d43c", + "revCount": 610, "submodules": true, "type": "git", "url": "https://dev.danilafe.com/Web-Projects/blog-static.git" @@ -18,25 +18,6 @@ "url": "https://dev.danilafe.com/Web-Projects/blog-static.git" } }, - "blog-source-localized": { - "flake": false, - "locked": { - "lastModified": 1599463261, - "narHash": "sha256-n6e4uRiOsWuOL7DSzbuP9X8cZNLnKOD/2/eOT0aB+Io=", - "ref": "localization", - "rev": "0b5748cc5a19b5d1d78bfcfa58d4027cc10524dd", - "revCount": 367, - "submodules": true, - "type": "git", - "url": "https://dev.danilafe.com/Web-Projects/blog-static.git" - }, - "original": { - "ref": "localization", - "submodules": true, - "type": "git", - "url": "https://dev.danilafe.com/Web-Projects/blog-static.git" - } - }, "flake-utils": { "locked": { "lastModified": 1648297722, @@ -169,7 +150,6 @@ "root": { "inputs": { "blog-source": "blog-source", - "blog-source-localized": "blog-source-localized", "flake-utils": "flake-utils", "katex-html": "katex-html", "nixpkgs": "nixpkgs_3" diff --git a/flake.nix b/flake.nix index f780acc..ea13de3 100644 --- a/flake.nix +++ b/flake.nix @@ -9,19 +9,12 @@ type = "git"; submodules = true; }; - blog-source-localized = { - flake = false; - url = "https://dev.danilafe.com/Web-Projects/blog-static.git"; - ref = "localization"; - type = "git"; - submodules = true; - }; }; - outputs = { self, blog-source, blog-source-localized, nixpkgs, flake-utils, katex-html }: + outputs = { self, blog-source, nixpkgs, flake-utils, katex-html }: let buildersFor = system: import ./lib.nix { - inherit blog-source blog-source-localized; + inherit blog-source; pkgs = import nixpkgs { inherit system; }; katex-html = katex-html.defaultPackage.${system}; }; @@ -30,6 +23,6 @@ inherit buildersFor; nixosModule = (import ./module.nix); } // flake-utils.lib.eachDefaultSystem (system: { - defaultPackage = (buildersFor system).english { host = "danilafe.com"; }; + defaultPackage = (buildersFor system).english { host = "danilafe.com"; }; }); } diff --git a/lib.nix b/lib.nix index 681bdf0..418f9ee 100644 --- a/lib.nix +++ b/lib.nix @@ -1,23 +1,24 @@ -{ blog-source, blog-source-localized, pkgs, katex-html }: +{ blog-source, pkgs, katex-html }: with pkgs; +with lib; let + protocol = ssl: if ssl then "https://" else "http://"; website = settings: stdenv.mkDerivation { inherit (settings) src ssl host; name = "blog-static"; - version = settings.src.rev; - postPatch = with settings.replaceUrl; '' - substituteInPlace config.toml --replace ${from} ${to} - ''; + version = settings.src.rev or "dirty"; publicPath = settings.path; - extraFlags = (if settings.drafts then " -D " else "") + settings.extraFlags; + hugoFlags = concatStringsSep " " ( + optionals settings.drafts (singleton "-D") ++ + [ "--baseURL=${protocol settings.ssl + settings.host}" ] + ); builder = ./build/builder.sh; buildInputs = [ - hugo katex-html + hugo ruby stork katex-html ]; }; - wrapHost = ssl: host: (if ssl then "https" else "http") + "://${host}"; in { english = settings: website { @@ -26,22 +27,5 @@ in drafts = settings.drafts or false; src = blog-source; path = "."; - extraFlags = "--config=config.toml,config-gen.toml"; - replaceUrl = { - from = "https://danilafe.com"; - to = wrapHost (settings.ssl or false) settings.host; - }; - }; - russian = settings: website { - inherit (settings) host; - ssl = settings.ssl or false; - drafts = settings.drafts or false; - src = blog-source-localized; - path = "ru"; - extraFlags = ""; - replaceUrl = { - from = "https://ru.danilafe.com"; - to = wrapHost (settings.ssl or false) settings.host; - }; }; }