1 { stdenv, lib, haskellPackages, writeText, gawk }:
3 awk = "${gawk}/bin/awk";
4 dockerCredentialsFile = import ./credentials.nix { inherit lib; };
8 , registry ? "https://registry-1.docker.io/v2/"
9 , repository ? "library"
16 # There must be no slashes in the repository or container names since
17 # we use these to make the output derivation name for the nix store
19 assert null == lib.findFirst (c: "/"==c) null (lib.stringToCharacters repository);
20 assert null == lib.findFirst (c: "/"==c) null (lib.stringToCharacters imageName);
22 # Only allow hocker-config and hocker-layer as fetchers for now
23 assert (builtins.elem fetcher ["hocker-config" "hocker-layer"]);
25 # If layerDigest is non-empty then it must not have a 'sha256:' prefix!
28 then !lib.hasPrefix "sha256:" layerDigest
33 lib.optionalString (layerDigest != "") "--layer ${layerDigest}";
37 builder = writeText "${fetcher}-builder.sh" ''
38 source "$stdenv/setup"
39 echo "${fetcher} exporting to $out"
43 # This is a hack for Hydra since we have no way of adding values
44 # to the NIX_PATH for Hydra jobsets!!
45 staticCredentialsFile="/etc/nix-docker-credentials.txt"
46 if [ ! -f "$dockerCredentialsFile" -a -f "$staticCredentialsFile" ]; then
47 echo "credentials file not set, falling back on static credentials file at: $staticCredentialsFile"
48 dockerCredentialsFile=$staticCredentialsFile
51 if [ -f "$dockerCredentialsFile" ]; then
52 echo "using credentials from $dockerCredentialsFile"
54 CREDSFILE=$(cat "$dockerCredentialsFile")
55 creds[token]=$(${awk} -F'=' '/DOCKER_TOKEN/ {print $2}' <<< "$CREDSFILE" | head -n1)
57 # Prefer DOCKER_TOKEN over the username and password
58 # authentication method
59 if [ -z "''${creds[token]}" ]; then
60 creds[user]=$(${awk} -F'=' '/DOCKER_USER/ {print $2}' <<< "$CREDSFILE" | head -n1)
61 creds[pass]=$(${awk} -F'=' '/DOCKER_PASS/ {print $2}' <<< "$CREDSFILE" | head -n1)
65 # These variables will be filled in first by the impureEnvVars, if
66 # those variables are empty then they will default to the
67 # credentials that may have been read in from the 'DOCKER_CREDENTIALS'
68 DOCKER_USER="''${DOCKER_USER:-''${creds[user]}}"
69 DOCKER_PASS="''${DOCKER_PASS:-''${creds[pass]}}"
70 DOCKER_TOKEN="''${DOCKER_TOKEN:-''${creds[token]}}"
72 ${fetcher} --out="$out" \
73 ''${registry:+--registry "$registry"} \
74 ''${DOCKER_USER:+--username "$DOCKER_USER"} \
75 ''${DOCKER_PASS:+--password "$DOCKER_PASS"} \
76 ''${DOCKER_TOKEN:+--token "$DOCKER_TOKEN"} \
78 "${repository}/${imageName}" \
82 buildInputs = [ haskellPackages.hocker ];
84 outputHashAlgo = "sha256";
85 outputHashMode = "flat";
88 preferLocalBuild = true;
90 impureEnvVars = [ "DOCKER_USER" "DOCKER_PASS" "DOCKER_TOKEN" ];
92 inherit registry dockerCredentialsFile;