Add derivation for static website

This commit is contained in:
Pim Kunis 2024-04-26 11:48:32 +02:00
parent f212aae139
commit 1410327b05
4 changed files with 155 additions and 4 deletions

1
.gitignore vendored
View file

@ -18,3 +18,4 @@ devenv.local.nix
.pre-commit-config.yaml .pre-commit-config.yaml
.jekyll-cache/ .jekyll-cache/
result

61
flake.lock Normal file
View file

@ -0,0 +1,61 @@
{
"nodes": {
"flutils": {
"inputs": {
"systems": "systems"
},
"locked": {
"lastModified": 1710146030,
"narHash": "sha256-SZ5L6eA7HJ/nmkzGG7/ISclqe6oZdOZTNoesiInkXPQ=",
"owner": "numtide",
"repo": "flake-utils",
"rev": "b1d9ab70662946ef0850d488da1c9019f3a9752a",
"type": "github"
},
"original": {
"owner": "numtide",
"repo": "flake-utils",
"type": "github"
}
},
"nixpkgs": {
"locked": {
"lastModified": 1714076141,
"narHash": "sha256-Drmja/f5MRHZCskS6mvzFqxEaZMeciScCTFxWVLqWEY=",
"owner": "nixos",
"repo": "nixpkgs",
"rev": "7bb2ccd8cdc44c91edba16c48d2c8f331fb3d856",
"type": "github"
},
"original": {
"owner": "nixos",
"ref": "nixos-unstable",
"repo": "nixpkgs",
"type": "github"
}
},
"root": {
"inputs": {
"flutils": "flutils",
"nixpkgs": "nixpkgs"
}
},
"systems": {
"locked": {
"lastModified": 1681028828,
"narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=",
"owner": "nix-systems",
"repo": "default",
"rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e",
"type": "github"
},
"original": {
"owner": "nix-systems",
"repo": "default",
"type": "github"
}
}
},
"root": "root",
"version": 7
}

View file

@ -3,13 +3,46 @@
inputs = { inputs = {
nixpkgs.url = "github:nixos/nixpkgs?ref=nixos-unstable"; nixpkgs.url = "github:nixos/nixpkgs?ref=nixos-unstable";
flutils.url = "github:numtide/flake-utils";
}; };
outputs = { self, nixpkgs }: { outputs = { self, nixpkgs, flutils }: flutils.lib.eachDefaultSystem (system:
let
pkgs = nixpkgs.legacyPackages.${system};
packages.x86_64-linux.hello = nixpkgs.legacyPackages.x86_64-linux.hello; gems = pkgs.bundlerEnv {
name = "blog-pim";
gemdir = ./src;
};
packages.x86_64-linux.default = self.packages.x86_64-linux.hello; patch-feed-date = pkgs.stdenv.mkDerivation {
name = "path-feed-date";
propagatedBuildInputs = [ pkgs.python3 ];
dontUnpack = true;
installPhase = "install -Dm755 ${./patch-feed-date.py} $out/bin/patch-feed-date";
};
in
{
packages.website = pkgs.stdenv.mkDerivation {
name = "blog-pim";
src = ./src;
sourceRoot = "src";
}; buildInputs = [
gems
gems.wrappedRuby
patch-feed-date
];
buildPhase = ''
bundle exec jekyll build
'';
installPhase = ''
mkdir -p $out
cp -r _site/* $out/
patch-feed-date --file _site/feed.xml > $out/feed.xml
'';
};
});
} }

56
patch-feed-date.py Executable file
View file

@ -0,0 +1,56 @@
#!/usr/bin/env python3
import argparse
import re
from datetime import datetime
updated_pattern = r"<updated>(?P<datetime>.{25})</updated>"
datetime_pattern = "%Y-%m-%dT%H:%M:%S%z"
def get_post_datetimes(feed):
regex = re.compile(updated_pattern)
datetimes = list()
first_skipped = False
for match in regex.finditer(feed):
# Skip first found datetime, as this is what we want to replace.
if not first_skipped:
first_skipped = True
continue
dt = datetime.strptime(match.group("datetime"), datetime_pattern)
datetimes.append(dt)
return datetimes
def main():
parser = argparse.ArgumentParser(
description="Patch the updated date field on an Atom feed."
)
parser.add_argument(
"--file", "-f", type=str, required=True, help="Path to the Atom feed file."
)
args = parser.parse_args()
with open(args.file, "r") as f:
feed = f.read()
datetimes = get_post_datetimes(feed)
newest_datetime = max(datetimes)
print(
re.sub(
updated_pattern,
f"<updated>{newest_datetime.strftime(datetime_pattern)}</updated>",
feed,
count=1,
),
end="",
)
if __name__ == "__main__":
main()