oci.nix (6056B)
1 { 2 lib, 3 pkgs, 4 }: 5 { 6 serviceName, 7 package, 8 binaryName, 9 buildInfo, 10 }: 11 assert lib.assertMsg pkgs.stdenv.isLinux "service OCI images require a Linux builder"; 12 assert lib.assertMsg (pkgs.stdenv.hostPlatform.isx86_64 13 ) "service OCI images support only x86_64-linux"; 14 assert lib.assertMsg ( 15 builtins.isString serviceName 16 && builtins.stringLength serviceName <= 128 17 && builtins.match "^[a-z][a-z0-9_]*$" serviceName != null 18 ) "serviceName must be a lowercase snake-case identifier"; 19 assert lib.assertMsg (lib.isDerivation package) "package must be a derivation"; 20 assert lib.assertMsg ( 21 (package.passthru.radrootsServicePackage.schema or null) == "radroots.service-package.v1" 22 && (package.passthru.radrootsServicePackage.servicePackage or null) == binaryName 23 && (package.passthru.radrootsServicePackage.binaryName or null) == binaryName 24 && (package.passthru.radrootsServicePackage.serviceLicense or null) == "AGPL-3.0-or-later" 25 ) "package must carry the governed Cargo-derived service identity and license"; 26 assert lib.assertMsg ( 27 builtins.isString binaryName 28 && builtins.stringLength binaryName <= 128 29 && builtins.match "^[a-z][a-z0-9_-]*$" binaryName != null 30 ) "binaryName must be a lowercase Cargo binary identifier"; 31 assert lib.assertMsg (builtins.isAttrs buildInfo) "buildInfo must be an attribute set"; 32 assert lib.assertMsg ( 33 builtins.attrNames buildInfo == [ 34 "contractVersions" 35 "featureProfile" 36 "libRevision" 37 "rustVersion" 38 "serviceCommit" 39 "serviceVersion" 40 "target" 41 ] 42 ) "buildInfo must contain exactly the governed service build fields"; 43 assert lib.assertMsg ( 44 builtins.isString buildInfo.serviceVersion 45 && builtins.stringLength buildInfo.serviceVersion <= 128 46 && builtins.match "^[A-Za-z0-9][A-Za-z0-9_.-]*$" buildInfo.serviceVersion != null 47 ) "buildInfo.serviceVersion must be a bounded image-tag-safe value"; 48 assert lib.assertMsg ( 49 builtins.isString buildInfo.serviceCommit 50 && builtins.match "^[0-9a-f]{40}$" buildInfo.serviceCommit != null 51 ) "buildInfo.serviceCommit must be a full lowercase Git revision"; 52 assert lib.assertMsg ( 53 builtins.isString buildInfo.libRevision 54 && builtins.match "^[0-9a-f]{40}$" buildInfo.libRevision != null 55 ) "buildInfo.libRevision must be a full lowercase Git revision"; 56 assert lib.assertMsg ( 57 buildInfo.rustVersion == "1.99.0" 58 ) "buildInfo.rustVersion must match the governed Rust toolchain"; 59 assert lib.assertMsg ( 60 buildInfo.target == pkgs.stdenv.hostPlatform.rust.rustcTarget 61 ) "buildInfo.target must match the package host platform"; 62 assert lib.assertMsg ( 63 buildInfo.featureProfile == "service-host" 64 ) "buildInfo.featureProfile must select the service-host profile"; 65 assert lib.assertMsg (builtins.isAttrs buildInfo.contractVersions) ( 66 "buildInfo.contractVersions must be an attribute set" 67 ); 68 assert lib.assertMsg ( 69 builtins.attrNames buildInfo.contractVersions == [ 70 "admin" 71 "config" 72 "provider" 73 "state" 74 "status" 75 ] 76 ) "buildInfo.contractVersions must contain exactly the governed contract versions"; 77 assert lib.assertMsg (lib.all 78 (version: builtins.isInt version && version > 0 && version <= 4294967295) 79 (builtins.attrValues buildInfo.contractVersions) 80 ) "every buildInfo contract version must be a positive u32"; 81 let 82 imageName = lib.replaceStrings [ "_" ] [ "-" ] serviceName; 83 user = "65532:65532"; 84 mountPaths = [ 85 "/etc/radroots/services/${serviceName}" 86 "/etc/radroots/secrets/services/${serviceName}" 87 "/run/radroots/services/${serviceName}" 88 "/var/lib/radroots/services/${serviceName}" 89 ]; 90 labels = { 91 "dev.radroots.build.feature-profile" = buildInfo.featureProfile; 92 "dev.radroots.build.lib-revision" = buildInfo.libRevision; 93 "dev.radroots.build.rust-version" = buildInfo.rustVersion; 94 "dev.radroots.build.target" = buildInfo.target; 95 "dev.radroots.contract.admin-version" = toString buildInfo.contractVersions.admin; 96 "dev.radroots.contract.config-version" = toString buildInfo.contractVersions.config; 97 "dev.radroots.contract.provider-version" = toString buildInfo.contractVersions.provider; 98 "dev.radroots.contract.state-version" = toString buildInfo.contractVersions.state; 99 "dev.radroots.contract.status-version" = toString buildInfo.contractVersions.status; 100 "dev.radroots.mount.config" = "/etc/radroots/services/${serviceName}"; 101 "dev.radroots.mount.config.mode" = "read-only"; 102 "dev.radroots.mount.credentials" = "/etc/radroots/secrets/services/${serviceName}"; 103 "dev.radroots.mount.credentials.mode" = "read-only"; 104 "dev.radroots.mount.runtime" = "/run/radroots/services/${serviceName}"; 105 "dev.radroots.mount.runtime.mode" = "read-write"; 106 "dev.radroots.mount.state" = "/var/lib/radroots/services/${serviceName}"; 107 "dev.radroots.mount.state.mode" = "read-write"; 108 "dev.radroots.rootfs" = "read-only-compatible"; 109 "org.opencontainers.image.description" = "Hardened ${serviceName} service image"; 110 "org.opencontainers.image.licenses" = package.passthru.radrootsServicePackage.serviceLicense; 111 "org.opencontainers.image.revision" = buildInfo.serviceCommit; 112 "org.opencontainers.image.title" = serviceName; 113 "org.opencontainers.image.version" = buildInfo.serviceVersion; 114 }; 115 in 116 pkgs.dockerTools.buildLayeredImage { 117 name = imageName; 118 tag = buildInfo.serviceVersion; 119 created = "1970-01-01T00:00:01Z"; 120 maxLayers = 2; 121 contents = [ 122 package 123 pkgs.dockerTools.caCertificates 124 ]; 125 config = { 126 User = user; 127 Entrypoint = [ "${package}/bin/${binaryName}" ]; 128 WorkingDir = "/"; 129 Env = [ "SSL_CERT_FILE=/etc/ssl/certs/ca-certificates.crt" ]; 130 StopSignal = "SIGTERM"; 131 Labels = labels; 132 }; 133 passthru.radrootsServiceOci = { 134 schema = "radroots.service-oci.v1"; 135 schemaVersion = 1; 136 inherit 137 buildInfo 138 imageName 139 labels 140 mountPaths 141 serviceName 142 user 143 ; 144 entrypoint = "${package}/bin/${binaryName}"; 145 }; 146 meta = { 147 description = "Hardened rootless OCI image for ${serviceName}"; 148 platforms = [ 149 "x86_64-linux" 150 ]; 151 }; 152 }