From f4c1887a42cc190f2d535eb4273f7d3533c8ef16 Mon Sep 17 00:00:00 2001 From: Sergei Poljanski Date: Tue, 17 Nov 2026 12:17:53 +0400 Subject: [PATCH] Build the site as a Nix package, with a NixOS module to serve it packages.default is the built site; nixosModules.default (services.blog) serves it from an nginx virtual host on a port, for Traefik to proxy. The servers will take it from here instead of the container image. CI now builds the package, so a change that breaks it never reaches main. The container deploy stays until the NixOS service has taken over. Assisted-by: Claude:opus-5-5 --- .forgejo/workflows/deploy.yaml | 4 +- .forgejo/workflows/test.yaml | 4 +- flake.nix | 69 +++++++++++++++++++++++++++++++--- 3 files changed, 68 insertions(+), 9 deletions(-) diff --git a/.forgejo/workflows/deploy.yaml b/.forgejo/workflows/deploy.yaml index 0c45c50..d037dbd 100644 --- a/.forgejo/workflows/deploy.yaml +++ b/.forgejo/workflows/deploy.yaml @@ -26,8 +26,8 @@ jobs: - name: Checkout uses: actions/checkout@v7.0.1 - - name: Build site - run: nix develop -c zola build + - name: Build site (the package nginx serves) + run: nix build .#default build: runs-on: arch-latest diff --git a/.forgejo/workflows/test.yaml b/.forgejo/workflows/test.yaml index e86840d..ad158e3 100644 --- a/.forgejo/workflows/test.yaml +++ b/.forgejo/workflows/test.yaml @@ -24,5 +24,5 @@ jobs: - name: Checkout uses: actions/checkout@v7.0.1 - - name: Build site - run: nix develop -c zola build + - name: Build site (the package nginx serves) + run: nix build .#default diff --git a/flake.nix b/flake.nix index f148a99..9c9fcb8 100644 --- a/flake.nix +++ b/flake.nix @@ -5,17 +5,76 @@ nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable"; }; - outputs = { self, nixpkgs }: + outputs = + { self, nixpkgs }: let system = "x86_64-linux"; pkgs = nixpkgs.legacyPackages.${system}; + inherit (nixpkgs) lib; in { - devShells.${system}.default = pkgs.mkShell { - buildInputs = [ - pkgs.zola - ]; + # The built site: the files nginx serves + packages.${system}.default = pkgs.stdenvNoCC.mkDerivation { + pname = "blog"; + version = self.shortRev or self.dirtyShortRev or "dev"; + src = lib.fileset.toSource { + root = ./.; + fileset = lib.fileset.unions [ + ./config.toml + ./content + ./static + ./templates + ]; + }; + nativeBuildInputs = [ pkgs.zola ]; + # zola 0.23 builds its HTTP client at start and panics without + # CA certificates, though the site fetches nothing + env.SSL_CERT_FILE = "${pkgs.cacert}/etc/ssl/certs/ca-bundle.crt"; + buildPhase = "zola build --output-dir $out"; + dontInstall = true; + }; + # services.blog: an nginx virtual host serving the site on a port; + # TLS and the public name are the proxy's (Traefik on asxpio-ops) + nixosModules.default = + { config, lib, ... }: + let + cfg = config.services.blog; + in + { + options.services.blog = { + enable = lib.mkEnableOption "blog.asxp.io"; + package = lib.mkOption { + type = lib.types.package; + default = self.packages.${system}.default; + defaultText = lib.literalExpression "the blog flake's package"; + }; + port = lib.mkOption { + type = lib.types.port; + default = 8081; + }; + }; + config = lib.mkIf cfg.enable { + services.nginx = { + enable = true; + virtualHosts."blog.asxp.io" = { + listen = [ + { + addr = "0.0.0.0"; + inherit (cfg) port; + } + ]; + root = cfg.package; + extraConfig = "error_page 404 /404.html;"; + locations."/".tryFiles = "$uri $uri/ =404"; + locations."~* \\.(css|png|svg|xml|ico)$".extraConfig = "expires 1h;"; + }; + }; + }; + }; + + devShells.${system}.default = pkgs.mkShell { + buildInputs = [ pkgs.zola ]; shellHook = '' echo "blog dev shell ready" echo " zola serve - live-reload preview on http://127.0.0.1:1111"