{
  description = "Le olde newspaper display";

  inputs.nixpkgs.url = "github:nixos/nixpkgs/release-25.05";
  inputs.flake-utils.url = "github:numtide/flake-utils";
  inputs.gitignore = {
    url = "github:hercules-ci/gitignore.nix";
    inputs.nixpkgs.follows = "nixpkgs";
  };
  inputs.opium = {
    url = "git+https://git.pbrinkmeier.de/paul/opium?ref=main";
    inputs.nixpkgs.follows = "nixpkgs";
    inputs.flake-utils.follows = "flake-utils";
  };

  outputs = { self, nixpkgs, flake-utils, gitignore, opium }:
  flake-utils.lib.eachSystem ["x86_64-linux" "aarch64-linux" "aarch64-darwin"] (system:
      let
        pkgs = import nixpkgs { inherit system; };

        inherit (pkgs.haskell.lib) compose;

        # Include opium in package set
        addOpium = self: super: {
          opium = opium.packages.${system}.opium;
        };

        # For building the package, we use only the files not ignored by Git as inputs.
        # Also, flake.nix, flake.lock and .gitea are not included to avoid rebuilds when
        # working on them.
        src = pkgs.lib.cleanSourceWith {
          src = ./.;
          filter = gitignore.lib.gitignoreFilterWith {
            basePath = ./.;
            extraRules = ''
              flake.nix
              flake.lock
              .gitea
            '';
          };
        };

        yore = pkgs.lib.pipe
          # The basic package
          (pkgs.haskellPackages.developPackage {
            root = src;
            overrides = addOpium;
          })
          # And some build configuration.
          # See https://nixos.org/manual/nixpkgs/unstable/#haskell-packaging-helpers.
          [
            # Remove warp which pulls GHC into the runtime deps for some reason.
            (compose.overrideCabal (drv: {
              postInstall = ''
                remove-references-to -t ${pkgs.haskellPackages.warp} $out/bin/yore
              '';
            }))
            # Return only the bin folder to curb image size
            compose.justStaticExecutables
          ];

        image =
          pkgs.dockerTools.buildImage {
            name = "git.pbrinkmeier.de/paul/yore";
            tag = yore.version;
            copyToRoot = [ pkgs.cacert ];
            config = {
              Cmd = [ "${entrypoint}/bin/run" ];
              StopSignal = "SIGINT";
            };
          };

        entrypoint = pkgs.writeShellApplication {
          name = "run";
          runtimeInputs = [ pkgs.coreutils pkgs.dbmate yore ];
          text = ''
            set -Eeuo pipefail

            mkdir -p "''${YORE_DBMATE_DIR}"
            TMPDIR=$(mktemp -dp "''${YORE_DBMATE_DIR}")
            mkdir -p "''${TMPDIR}/db"
            cp -r ${./db}/* "''${TMPDIR}/db"
            cd "''${TMPDIR}"
            dbmate up
            chmod +w -R "''${TMPDIR}"
            rm -rf "''${TMPDIR}"

            cp -r ${./static} static

            # Replace bash and inherit PID 1
            cd /
            exec yore
          '';
        };
      in {
        packages = rec {
          default = yore;
          inherit yore image;
          yore-meta = pkgs.runCommand "yore-meta" {} ''
            mkdir -p $out
            echo -n ${yore.version} > $out/version
            echo -n ${image.drvAttrs.imageName}:${image.drvAttrs.imageTag} > $out/image-tag
          '';


          opium_ = opium.packages.${system}.opium;
        };

        devShells = {
          default =
            (pkgs.haskellPackages.developPackage {
              root = ./.;
              modifier = drv:
                pkgs.haskell.lib.addBuildTools drv [
                  pkgs.cabal-install
                  pkgs.haskellPackages.implicit-hie
                  pkgs.haskellPackages.hoogle
                  pkgs.haskell-language-server
                  pkgs.fourmolu
                  pkgs.postgresql
                  pkgs.dbmate
                ];
              overrides = addOpium;
            }).env;
        };
        apps = {
          crane = {
            type = "app";
            program = "${pkgs.crane}/bin/crane";
          };
        };
      });
}
