Plugin to add Pijul support to the Nix package manager
{
  inputs = {
    nixpkgs.url = "nixpkgs/nixpkgs-unstable";
  };

  outputs = {
    self,
    nixpkgs,
  }: let
    systems = ["x86_64-linux" "aarch64-linux" "x86_64-darwin" "aarch64-darwin"];
    eachSystem = f: nixpkgs.lib.genAttrs systems (system: f nixpkgs.legacyPackages.${system});
  in {
    formatter = eachSystem (pkgs: pkgs.alejandra);

    packages = eachSystem (pkgs: rec {
      default = nix-plugin-pijul;

      nix-plugin-pijul = pkgs.callPackage ./package.nix {nix = pkgs.nixVersions.latest;};
    });

    checks = eachSystem (pkgs: let
      localRepoCheck = nix:
        pkgs.runCommand "localRepoCheck-${nix.name}"
        {
          nativeBuildInputs = [
            pkgs.pijul
            nix
          ];
        }
        ''
          export HOME=$(mktemp -d)
          export EDITOR=true
          pijul identity new --no-link --no-prompt --display-name 'Test User' --email 'test@example.com'

          pijul init repo
          cd repo

          echo "it works" > foo
          pijul add foo
          pijul record --message 'Add foo'

          output=$(
            nix \
              --option plugin-files ${self.packages.${pkgs.system}.nix-plugin-pijul.override {inherit nix;}}/lib/nix/plugins/pijul.so \
              --extra-experimental-features 'nix-command flakes' \
              eval --impure --raw --expr "builtins.readFile ((builtins.fetchTree \"pijul+file://$PWD\") + \"/foo\")"
          )

          echo $output

          [[ "$output" = "it works" ]]

          mkdir $out
        '';
    in {
      nix_2_18 = localRepoCheck pkgs.nixVersions.nix_2_18;
      nix_2_19 = localRepoCheck pkgs.nixVersions.nix_2_19;
      nix_2_20 = localRepoCheck pkgs.nixVersions.nix_2_20;
      nix_2_21 = localRepoCheck pkgs.nixVersions.nix_2_21;
      nix_2_22 = localRepoCheck pkgs.nixVersions.nix_2_22;
      nix_2_23 = localRepoCheck pkgs.nixVersions.nix_2_23;
      nix_2_24 = localRepoCheck pkgs.nixVersions.nix_2_24;

      stable = localRepoCheck pkgs.nixVersions.stable;
      latest = localRepoCheck pkgs.nixVersions.latest;
    });
  };
}