package.nix (2719B)
1 { 2 crane, 3 lib, 4 pkgs, 5 }: 6 { 7 toolchain, 8 source, 9 cargoLock, 10 servicePackage, 11 binaryName ? servicePackage, 12 nativeInputs, 13 releaseProfile ? "release", 14 }: 15 assert lib.assertMsg (lib.isDerivation toolchain) "toolchain must be a derivation"; 16 assert lib.assertMsg (builtins.pathExists source) "source must exist"; 17 assert lib.assertMsg (builtins.pathExists cargoLock) "cargoLock must exist"; 18 assert lib.assertMsg ( 19 builtins.isString servicePackage && builtins.match "^[a-z][a-z0-9_-]*$" servicePackage != null 20 ) "servicePackage must be a lowercase Cargo package identifier"; 21 assert lib.assertMsg ( 22 builtins.isString binaryName && builtins.match "^[a-z][a-z0-9_-]*$" binaryName != null 23 ) "binaryName must be a lowercase Cargo binary identifier"; 24 assert lib.assertMsg ( 25 builtins.isString releaseProfile 26 && builtins.match "^release(-[a-z0-9][a-z0-9_-]*)?$" releaseProfile != null 27 ) "releaseProfile must be release or a release-prefixed Cargo profile"; 28 assert lib.assertMsg ( 29 builtins.isAttrs nativeInputs 30 && (nativeInputs.schema or null) == "radroots.service.native-inputs.v1" 31 && builtins.isList (nativeInputs.nativeBuildInputs or null) 32 && builtins.isList (nativeInputs.buildInputs or null) 33 && builtins.isAttrs (nativeInputs.environment or null) 34 ) "nativeInputs must come from mkNativeInputs"; 35 assert lib.assertMsg ( 36 !(builtins.hasAttr "CARGO_PROFILE" nativeInputs.environment) 37 ) "nativeInputs.environment must not replace CARGO_PROFILE"; 38 let 39 manifest = builtins.fromTOML (builtins.readFile (source + "/Cargo.toml")); 40 serviceLicense = 41 if manifest ? workspace && manifest.workspace ? package then 42 manifest.workspace.package.license or null 43 else 44 manifest.package.license or null; 45 _licenseAssertion = assert lib.assertMsg ( 46 serviceLicense == "AGPL-3.0-or-later" 47 ) "service source must derive the governed AGPL-3.0-or-later license from Cargo.toml"; true; 48 craneLib = (crane.mkLib pkgs).overrideToolchain toolchain; 49 cargoExtraArgs = "--locked --package ${servicePackage} --bin ${binaryName}"; 50 commonArgs = { 51 src = craneLib.cleanCargoSource source; 52 inherit cargoLock cargoExtraArgs; 53 CARGO_PROFILE = releaseProfile; 54 strictDeps = true; 55 nativeBuildInputs = nativeInputs.nativeBuildInputs; 56 buildInputs = nativeInputs.buildInputs; 57 env = nativeInputs.environment; 58 doCheck = false; 59 }; 60 cargoArtifacts = craneLib.buildDepsOnly commonArgs; 61 in 62 assert _licenseAssertion; 63 craneLib.buildPackage ( 64 commonArgs 65 // { 66 inherit cargoArtifacts; 67 passthru.radrootsServicePackage = { 68 inherit binaryName serviceLicense servicePackage; 69 schema = "radroots.service-package.v1"; 70 }; 71 meta.license = lib.licenses.agpl3Plus; 72 } 73 )