summaryrefslogtreecommitdiff
path: root/flake.nix
blob: 1ca32d2985c71440e4b2cef34434bef01987e447 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
{
  description = "Run the PostgreSQL server off a temporary data directory";

  inputs = {
    nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
    flake-parts.url = "github:hercules-ci/flake-parts";
    crane.url = "github:ipetkov/crane";
  };

  outputs =
    inputs@{
      self,
      nixpkgs,
      flake-parts,
      crane,
      ...
    }:
    flake-parts.lib.mkFlake { inherit inputs; } {
      systems = [
        "x86_64-linux"
        "aarch64-linux"
        "aarch64-darwin"
      ];

      perSystem =
        {
          self',
          pkgs,
          lib,
          ...
        }:
        let
          craneLib = crane.mkLib pkgs;
          src = craneLib.cleanCargoSource ./.;
          commonArgs = {
            inherit src;
            strictDeps = true;
          };
          cargoArtifacts = craneLib.buildDepsOnly commonArgs;
          crate = craneLib.buildPackage (
            commonArgs
            // {
              inherit cargoArtifacts;
              nativeBuildInputs = [
                pkgs.coreutils
                pkgs.postgresql
              ];
              meta.mainProgram = "temp-postgres";
              VERGEN_GIT_SHA =
                if self ? shortRev then
                  self.shortRev
                else if self ? dirtyShortRev then
                  lib.strings.removeSuffix "-dirty" self.dirtyShortRev
                else
                  "unknown";
              VERGEN_GIT_DIRTY = if self ? dirtyShortRev then "true" else "false";
            }
          );
          version = crate.version;
          revision = self.shortRev or self.dirtyShortRev or "unknown";
          wrap =
            {
              postgresql,
              suffix ? "",
            }:
            pkgs.stdenv.mkDerivation {
              name = "temp-postgres-${version}-${revision}${suffix}";
              version = "${version}-${revision}";
              nativeBuildInputs = [ pkgs.makeBinaryWrapper ];
              buildCommand = ''
                mkdir -p $out/bin
                makeWrapper ${crate}/bin/temp-postgres $out/bin/temp-postgres \
                  --prefix PATH : ${lib.makeBinPath [ postgresql ]}
              '';
            };
          postgresqlVersions = lib.mapAttrs' (
            name: drv:
            let
              postgresqlVersion = lib.removePrefix "postgresql_" name;
            in
            lib.nameValuePair "temp-postgres_${postgresqlVersion}" (wrap {
              postgresql = drv;
              suffix = "_${postgresqlVersion}";
            })
          ) pkgs.postgresqlVersions;
        in
        {
          packages = {
            default = self'.packages.temp-postgres;
            temp-postgres = wrap { postgresql = pkgs.postgresql; };
            temp-postgres-unwrapped = crate;
          }
          // postgresqlVersions;

          devShells.default = pkgs.mkShell {
            nativeBuildInputs = with pkgs; [
              cargo
              cargo-audit
              cargo-deny
              cargo-edit
              clippy
              just
              nixfmt
              postgresql
              rust-analyzer
              rustc
              rustfmt
              taplo
              watchexec
            ];
            shellHook = ''
              export FLAKE=${self}
            '';
          };

          formatter = pkgs.nixfmt-tree;
        };
    };
}
Generated by cgit. See skreutz.com for my tech blog and contact information.