feat: improve caching

This commit is contained in:
TECHNOFAB 2024-09-13 18:43:56 +00:00
parent a2a7f7c4ed
commit 78e6c5e278
4 changed files with 149 additions and 47 deletions

View file

@ -33,19 +33,46 @@
nix-jobs-per-default = mkOption {
type = types.bool;
default = true;
description = "Handle jobs nix-based by default or via opt-in (in job set nix = true) if false";
description = "Handle jobs nix-based by default or via opt-in (in a job set nix.enable = true) if false";
};
disable-cache = mkOption {
type = types.bool;
default = false;
description = "Whether to remove the cache key from all nix jobs and set NIX_CI_DISABLE_CACHE";
};
cache-key = mkOption {
type = types.str;
default = "$CI_JOB_NAME-$CI_COMMIT_REF_SLUG";
description = "Cache key to use for the nix cache";
};
};
jobType = with lib;
subType {
# nix ci opts
nix = mkOption {
type = types.bool;
default = cfg.nix-jobs-per-default;
};
deps = mkOption {
type = types.listOf types.package;
default = [];
type = subType {
enable = mkOption {
type = types.bool;
default = cfg.nix-jobs-per-default;
description = "Handle this job as a nix job";
};
deps = mkOption {
type = types.listOf types.package;
default = [];
description = "Dependencies/packages to install for this job";
};
disable-cache = mkOption {
type = types.bool;
default = cfg.disable-cache;
description = "Whether to remove the cache key from this job and set NIX_CI_DISABLE_CACHE";
};
cache-key = mkOption {
type = types.str;
default = cfg.cache-key;
description = "Cache key to use for the nix cache";
};
};
description = "Configure Nix Gitlab CI for each job individually";
};
# gitlab opts
script = mkOption {
@ -64,7 +91,7 @@
allow_failure = mkNullOption (types.either types.attrs types.bool);
artifacts = mkNullOption (types.attrs);
before_script = mkNullOption (types.listOf types.str);
cache = mkNullOption (types.attrs);
cache = mkNullOption (types.either (types.listOf types.attrs) types.attrs);
coverage = mkNullOption (types.str);
dependencies = mkNullOption (types.listOf types.str);
environment = mkNullOption (types.either types.attrs types.str);
@ -124,7 +151,7 @@
mapAttrs = cb: set: builtins.listToAttrs (builtins.map (key: cb key (builtins.getAttr key set)) (builtins.attrNames set));
prepend = key: arr: job:
job
// lib.optionalAttrs job.nix {
// lib.optionalAttrs job.nix.enable {
${key} =
arr
++ job.${key} or [];
@ -143,11 +170,9 @@
variablesWithStorePaths =
lib.concatMapAttrs (
name: value:
if lib.hasInfix "/nix/store/" value
then {
lib.optionalAttrs (lib.hasInfix "/nix/store/" value) {
${name} = value;
}
else {}
)
(job.variables or {});
variableExports = lib.concatMapStrings (x: "${x}\n") (
@ -156,7 +181,7 @@
in {
name = "gitlab-ci-job-deps:${key}";
value = pkgs.writeShellScript "gitlab-ci-job-deps:${key}" ''
export PATH="${lib.makeBinPath job.deps}:$PATH";
export PATH="${lib.makeBinPath job.nix.deps}:$PATH";
${variableExports}
'';
})
@ -181,17 +206,32 @@
"finalize_nix_ci"
]
job))
// lib.optionalAttrs job.nix {
// lib.optionalAttrs job.nix.enable {
image = job.image;
variables = lib.concatMapAttrs (name: value:
if lib.hasInfix "/nix/store/" value
then {}
else {
${name} = value;
})
(job.variables or {});
variables =
lib.concatMapAttrs (name: value:
lib.optionalAttrs (!lib.hasInfix "/nix/store/" value) {
${name} = value;
})
(job.variables or {})
// lib.optionalAttrs job.nix.disable-cache {
NIX_CI_DISABLE_CACHE = "yes";
};
cache =
(
let
c = job.cache or [];
in
if builtins.isList c
then c
else [c]
)
++ (lib.optional (!job.nix.disable-cache) {
key = job.nix.cache-key;
paths = [".nix-cache/"];
});
}
) ["nix" "deps"];
) ["nix"];
})
jobs;
in