From 1410327b055bf69716aaa45bfefb3d1ddd990cef Mon Sep 17 00:00:00 2001 From: Pim Kunis Date: Fri, 26 Apr 2024 11:48:32 +0200 Subject: [PATCH] Add derivation for static website --- .gitignore | 1 + flake.lock | 61 ++++++++++++++++++++++++++++++++++++++++++++++ flake.nix | 41 ++++++++++++++++++++++++++++--- patch-feed-date.py | 56 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 155 insertions(+), 4 deletions(-) create mode 100644 flake.lock create mode 100755 patch-feed-date.py diff --git a/.gitignore b/.gitignore index 7166508..ad4e0ed 100644 --- a/.gitignore +++ b/.gitignore @@ -18,3 +18,4 @@ devenv.local.nix .pre-commit-config.yaml .jekyll-cache/ +result diff --git a/flake.lock b/flake.lock new file mode 100644 index 0000000..bc67a31 --- /dev/null +++ b/flake.lock @@ -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 +} diff --git a/flake.nix b/flake.nix index c7a9a1c..03c1d4d 100644 --- a/flake.nix +++ b/flake.nix @@ -3,13 +3,46 @@ inputs = { 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 + ''; + }; + }); } diff --git a/patch-feed-date.py b/patch-feed-date.py new file mode 100755 index 0000000..d8cfb15 --- /dev/null +++ b/patch-feed-date.py @@ -0,0 +1,56 @@ +#!/usr/bin/env python3 + +import argparse +import re +from datetime import datetime + +updated_pattern = r"(?P.{25})" +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"{newest_datetime.strftime(datetime_pattern)}", + feed, + count=1, + ), + end="", + ) + + +if __name__ == "__main__": + main()