|
| 1 | +{ |
| 2 | + lib, |
| 3 | + pkgs, |
| 4 | + go, |
| 5 | +}: |
| 6 | +let |
| 7 | + sanitizePackage = |
| 8 | + package: |
| 9 | + let |
| 10 | + raw = |
| 11 | + if package == "." || package == "./" then |
| 12 | + "root" |
| 13 | + else |
| 14 | + lib.replaceStrings |
| 15 | + [ |
| 16 | + "./" |
| 17 | + "/" |
| 18 | + "." |
| 19 | + "*" |
| 20 | + ] |
| 21 | + [ |
| 22 | + "" |
| 23 | + "-" |
| 24 | + "-" |
| 25 | + "all" |
| 26 | + ] |
| 27 | + package; |
| 28 | + in |
| 29 | + if raw == "" then "root" else raw; |
| 30 | + |
| 31 | + commonArgs = |
| 32 | + args: |
| 33 | + let |
| 34 | + src = args.src or (throw "goUnit.buildWorkspace requires src"); |
| 35 | + modRoot = args.modRoot or "."; |
| 36 | + moduleRoot = if modRoot == "." then src else src + "/${modRoot}"; |
| 37 | + goMod = args.goMod or (moduleRoot + "/go.mod"); |
| 38 | + goSum = args.goSum or (moduleRoot + "/go.sum"); |
| 39 | + in |
| 40 | + assert lib.assertMsg (builtins.pathExists goMod) |
| 41 | + "goUnit.buildWorkspace requires a checked-in go.mod at ${builtins.toString goMod}"; |
| 42 | + assert lib.assertMsg (builtins.pathExists goSum) |
| 43 | + "goUnit.buildWorkspace requires a checked-in go.sum lockfile at ${builtins.toString goSum}"; |
| 44 | + { |
| 45 | + pname = args.pname or "go-unit"; |
| 46 | + version = args.version or "0.0.0"; |
| 47 | + inherit |
| 48 | + src |
| 49 | + goMod |
| 50 | + goSum |
| 51 | + modRoot |
| 52 | + moduleRoot |
| 53 | + ; |
| 54 | + vendorHash = |
| 55 | + args.vendorHash or (throw "goUnit.buildWorkspace requires vendorHash from pkgs.buildGoModule"); |
| 56 | + packages = |
| 57 | + let |
| 58 | + packages = args.packages or [ "." ]; |
| 59 | + in |
| 60 | + if packages == [ ] then throw "goUnit.buildWorkspace requires at least one package" else packages; |
| 61 | + goToolchain = args.goToolchain or go.toolchain pkgs { version = "latest"; }; |
| 62 | + nativeBuildInputs = args.nativeBuildInputs or [ ]; |
| 63 | + buildInputs = args.buildInputs or [ ]; |
| 64 | + env = args.env or { }; |
| 65 | + ldflags = args.ldflags or [ ]; |
| 66 | + tags = args.tags or [ ]; |
| 67 | + }; |
| 68 | + |
| 69 | + buildPackage = |
| 70 | + args: package: |
| 71 | + let |
| 72 | + buildGoModule = pkgs.buildGoModule.override { go = args.goToolchain; }; |
| 73 | + in |
| 74 | + buildGoModule { |
| 75 | + pname = "${args.pname}-${sanitizePackage package}"; |
| 76 | + inherit (args) |
| 77 | + version |
| 78 | + vendorHash |
| 79 | + goSum |
| 80 | + nativeBuildInputs |
| 81 | + buildInputs |
| 82 | + env |
| 83 | + ldflags |
| 84 | + tags |
| 85 | + ; |
| 86 | + src = args.moduleRoot; |
| 87 | + modRoot = "."; |
| 88 | + subPackages = [ package ]; |
| 89 | + doCheck = false; |
| 90 | + strictDeps = true; |
| 91 | + passthru.goUnit = { |
| 92 | + inherit (args) goSum goToolchain env; |
| 93 | + inherit package; |
| 94 | + }; |
| 95 | + }; |
| 96 | + |
| 97 | + testPackage = |
| 98 | + args: package: |
| 99 | + let |
| 100 | + buildGoModule = pkgs.buildGoModule.override { go = args.goToolchain; }; |
| 101 | + in |
| 102 | + buildGoModule { |
| 103 | + pname = "${args.pname}-${sanitizePackage package}-test"; |
| 104 | + inherit (args) |
| 105 | + version |
| 106 | + vendorHash |
| 107 | + goSum |
| 108 | + nativeBuildInputs |
| 109 | + buildInputs |
| 110 | + env |
| 111 | + ldflags |
| 112 | + tags |
| 113 | + ; |
| 114 | + src = args.moduleRoot; |
| 115 | + modRoot = "."; |
| 116 | + subPackages = [ package ]; |
| 117 | + doCheck = true; |
| 118 | + strictDeps = true; |
| 119 | + installPhase = '' |
| 120 | + mkdir -p "$out" |
| 121 | + touch "$out/done" |
| 122 | + ''; |
| 123 | + passthru.goUnit = { |
| 124 | + inherit (args) goSum goToolchain env; |
| 125 | + inherit package; |
| 126 | + }; |
| 127 | + }; |
| 128 | + |
| 129 | + /** |
| 130 | + Build and test a locked Go module as package-shaped Nix derivations. |
| 131 | +
|
| 132 | + Go does not expose Cargo's rustc unit graph, so callers choose the package |
| 133 | + patterns that deserve independent cache and test boundaries. The helper |
| 134 | + requires `go.mod`, `go.sum`, and `vendorHash`. |
| 135 | +
|
| 136 | + Arguments: |
| 137 | + - `src`: filtered module source. |
| 138 | + - `packages`: Go package patterns to expose, default `[ "." ]`. |
| 139 | + - `vendorHash`: hash accepted by `pkgs.buildGoModule`. |
| 140 | + - `goToolchain`: optional Go package, default `ix.languages.go.toolchain pkgs { version = "latest"; }`. |
| 141 | +
|
| 142 | + Returns `packages`, `tests`, `default`, `checks`, and `sourceAudit`. |
| 143 | + */ |
| 144 | + buildWorkspace = |
| 145 | + rawArgs: |
| 146 | + let |
| 147 | + args = commonArgs rawArgs; |
| 148 | + packageNames = map sanitizePackage args.packages; |
| 149 | + uniquePackageNames = lib.unique packageNames; |
| 150 | + packageAttrs = lib.listToAttrs ( |
| 151 | + lib.zipListsWith ( |
| 152 | + name: package: lib.nameValuePair name (buildPackage args package) |
| 153 | + ) packageNames args.packages |
| 154 | + ); |
| 155 | + testAttrs = lib.listToAttrs ( |
| 156 | + lib.zipListsWith ( |
| 157 | + name: package: lib.nameValuePair name (testPackage args package) |
| 158 | + ) packageNames args.packages |
| 159 | + ); |
| 160 | + in |
| 161 | + assert lib.assertMsg (builtins.length uniquePackageNames == builtins.length packageNames) |
| 162 | + "goUnit.buildWorkspace package patterns must sanitize to unique names: ${lib.concatStringsSep ", " args.packages}"; |
| 163 | + { |
| 164 | + packages = packageAttrs; |
| 165 | + tests = testAttrs; |
| 166 | + checks = testAttrs; |
| 167 | + default = packageAttrs.${builtins.head packageNames}; |
| 168 | + sourceAudit = { |
| 169 | + module = { |
| 170 | + base = "workspace"; |
| 171 | + scope = "module"; |
| 172 | + relative = args.modRoot; |
| 173 | + lockFile = if builtins.pathExists args.goSum then "go.sum" else null; |
| 174 | + }; |
| 175 | + }; |
| 176 | + }; |
| 177 | +in |
| 178 | +{ |
| 179 | + inherit buildWorkspace; |
| 180 | +} |
0 commit comments