mirror of
https://gitlab.com/TECHNOFAB/nix-gitlab-ci.git
synced 2025-12-12 02:00:13 +01:00
feat: initial v3 rewrite
This commit is contained in:
commit
0952ab4145
32 changed files with 1457 additions and 0 deletions
48
nix/packages/pkgs.nix
Normal file
48
nix/packages/pkgs.nix
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
{
|
||||
inputs,
|
||||
system,
|
||||
...
|
||||
}: let
|
||||
inherit (inputs) pkgs;
|
||||
in rec {
|
||||
setupScript = pkgs.writeShellScriptBin "setup_nix_ci" (builtins.readFile ./scripts/setup_nix_ci.sh);
|
||||
finalizeScript = pkgs.writeShellScriptBin "finalize_nix_ci" (builtins.readFile ./scripts/finalize_nix_ci.sh);
|
||||
image = pkgs.dockerTools.buildImage {
|
||||
name = "nix-ci";
|
||||
fromImage = let
|
||||
hashes = {
|
||||
"x86_64-linux" = "sha256-kJ7dqje5o1KPr3RDZ7/THbhMSoiCU1C/7HshDrNfwnM=";
|
||||
"aarch64-linux" = "sha256-jz+Z3Ji+hy5d9ImOh/YOKCqy9P9/cseSov+5J/O95bg=";
|
||||
};
|
||||
# check digest of tags like nixos-24.11-aarch64-linux etc.
|
||||
digests = {
|
||||
"x86_64-linux" = "sha256:345f210dea4cbd049e2d01d13159c829066dfb6e273cdd49ea878186d17b19f7";
|
||||
"aarch64-linux" = "sha256:66163fdf446d851416dd4e9be28c0794d9c2550214a57a846957699a3f5747f6";
|
||||
};
|
||||
hash = hashes.${system} or (throw "Unsupported system");
|
||||
imageDigest = digests.${system} or (throw "Unsupported system");
|
||||
in
|
||||
pkgs.dockerTools.pullImage {
|
||||
imageName = "nixpkgs/nix-flakes";
|
||||
inherit hash imageDigest;
|
||||
};
|
||||
copyToRoot = pkgs.buildEnv {
|
||||
name = "image-root";
|
||||
paths = with pkgs;
|
||||
[
|
||||
gitMinimal
|
||||
gnugrep
|
||||
gnused
|
||||
coreutils
|
||||
diffutils
|
||||
cachix
|
||||
attic-client
|
||||
]
|
||||
++ [
|
||||
setupScript
|
||||
finalizeScript
|
||||
];
|
||||
pathsToLink = ["/bin"];
|
||||
};
|
||||
};
|
||||
}
|
||||
40
nix/packages/scripts/finalize_nix_ci.sh
Normal file
40
nix/packages/scripts/finalize_nix_ci.sh
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
echo -e "\\e[0Ksection_start:`date +%s`:finalize_nix_ci[collapsed=true]\\r\\e[0KFinalizing Nix CI..."
|
||||
nix path-info --all > /tmp/nix-store-after
|
||||
echo "Finding new paths..."
|
||||
NEW_PATHS=$(diff --new-line-format="%L" \
|
||||
--old-line-format="" --unchanged-line-format="" \
|
||||
/tmp/nix-store-before /tmp/nix-store-after)
|
||||
COUNT=$(wc -l <<<"$NEW_PATHS")
|
||||
|
||||
if [[ "$NIX_CI_CACHE_STRATEGY" == "auto" ]]; then
|
||||
export NIX_CI_CACHE_STRATEGY="${NIX_CI_RUNNER_CACHE_STRATEGY:-${NIX_CI_DEFAULT_CACHE_STRATEGY:-none}}";
|
||||
fi
|
||||
|
||||
if [ -z "$NIX_CI_DISABLE_CACHE" ]; then
|
||||
echo -e "\\e[0Ksection_start:`date +%s`:cache_push[collapsed=true]\\r\\e[0KPushing $COUNT new store paths to cache ($NIX_CI_CACHE_STRATEGY)"
|
||||
echo -n "$NEW_PATHS" | {
|
||||
case "$NIX_CI_CACHE_STRATEGY" in
|
||||
"runner")
|
||||
export RUNNER_CACHE=''${RUNNER_CACHE:-"file://$(pwd)/.nix-cache"}
|
||||
# add ^* to all store paths ending in .drv (prevent warning log spam)
|
||||
sed '/\.drv$/s/$/^*/' | nix copy --quiet --to "$RUNNER_CACHE" --stdin || true
|
||||
;;
|
||||
"attic")
|
||||
attic push --stdin ci:$ATTIC_CACHE || true
|
||||
;;
|
||||
"cachix")
|
||||
cachix push $CACHIX_CACHE || true
|
||||
;;
|
||||
"none")
|
||||
echo "Cache strategy is none, doing nothing..."
|
||||
;;
|
||||
*)
|
||||
echo "WARNING: Invalid cache strategy set: '$NIX_CI_CACHE_STRATEGY'"
|
||||
;;
|
||||
esac
|
||||
}
|
||||
echo -e "\\e[0Ksection_end:`date +%s`:cache_push\\r\\e[0K"
|
||||
else
|
||||
echo "Caching disabled, not uploading $COUNT new store entries..."
|
||||
fi
|
||||
echo -e "\\e[0Ksection_end:`date +%s`:finalize_nix_ci\\r\\e[0K"
|
||||
48
nix/packages/scripts/setup_nix_ci.sh
Normal file
48
nix/packages/scripts/setup_nix_ci.sh
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
echo -e "\\e[0Ksection_start:`date +%s`:nix_setup[collapsed=true]\\r\\e[0KSetting up Nix CI"
|
||||
nix path-info --all > /tmp/nix-store-before
|
||||
|
||||
if [[ "$NIX_CI_CACHE_STRATEGY" == "auto" ]]; then
|
||||
export NIX_CI_CACHE_STRATEGY="${NIX_CI_RUNNER_CACHE_STRATEGY:-${NIX_CI_DEFAULT_CACHE_STRATEGY:-none}}";
|
||||
echo "NIX_CI_CACHE_STRATEGY was set to auto, selected '$NIX_CI_CACHE_STRATEGY' for this job"
|
||||
fi
|
||||
|
||||
if [ -z "$NIX_CI_DISABLE_CACHE" ]; then
|
||||
echo -e "\\e[0Ksection_start:`date +%s`:cache_setup[collapsed=true]\\r\\e[0KConfiguring cache ($NIX_CI_CACHE_STRATEGY)"
|
||||
case "$NIX_CI_CACHE_STRATEGY" in
|
||||
"runner")
|
||||
export RUNNER_CACHE=''${RUNNER_CACHE:-"file://$(pwd)/.nix-cache"}
|
||||
echo "Runner Cache: $RUNNER_CACHE"
|
||||
export NIX_CONFIG="$NIX_CONFIG
|
||||
extra-trusted-substituters = $RUNNER_CACHE?priority=10&trusted=true
|
||||
extra-substituters = $RUNNER_CACHE?priority=10&trusted=true
|
||||
"
|
||||
;;
|
||||
"attic")
|
||||
echo "Attic Cache: $ATTIC_CACHE"
|
||||
attic login --set-default ci "$ATTIC_SERVER" "$ATTIC_TOKEN" || true
|
||||
attic use "$ATTIC_CACHE" || true
|
||||
;;
|
||||
"cachix")
|
||||
echo "Cachix Cache: $CACHIX_CACHE"
|
||||
cachix use "$CACHIX_CACHE" || true
|
||||
;;
|
||||
"none")
|
||||
echo "Cache strategy is none, doing nothing..."
|
||||
;;
|
||||
*)
|
||||
echo "WARNING: Invalid cache strategy set: '$NIX_CI_CACHE_STRATEGY'"
|
||||
;;
|
||||
esac
|
||||
echo -e "\\e[0Ksection_end:`date +%s`:cache_setup\\r\\e[0K"
|
||||
else
|
||||
echo "Caching disabled (NIX_CI_DISABLE_CACHE), skipping cache configuration..."
|
||||
fi
|
||||
|
||||
# load the job's deps only if the name was passed
|
||||
if [[ ! -z $1 ]]; then
|
||||
echo -e "\\e[0Ksection_start:`date +%s`:nix_deps[collapsed=true]\\r\\e[0KFetching Nix dependencies for job"
|
||||
nix build .#$1
|
||||
source $(readlink -f result)
|
||||
echo -e "\\e[0Ksection_end:`date +%s`:nix_deps\\r\\e[0K"
|
||||
fi
|
||||
echo -e "\\e[0Ksection_end:`date +%s`:nix_setup\\r\\e[0K"
|
||||
45
nix/repo/ci.nix
Normal file
45
nix/repo/ci.nix
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
{inputs, ...}: let
|
||||
inherit (inputs) cilib;
|
||||
in
|
||||
cilib.mkCI {
|
||||
pipelines."default" = {
|
||||
stages = ["test" "build" "deploy"];
|
||||
jobs = {
|
||||
"test" = {
|
||||
stage = "test";
|
||||
script = [
|
||||
"nix run .#tests -- --junit=junit.xml"
|
||||
];
|
||||
allow_failure = true;
|
||||
artifacts = {
|
||||
when = "always";
|
||||
reports.junit = "junit.xml";
|
||||
};
|
||||
};
|
||||
"docs" = {
|
||||
stage = "build";
|
||||
script = [
|
||||
# sh
|
||||
''
|
||||
nix build .#docs:default
|
||||
mkdir -p public
|
||||
cp -r result/. public/
|
||||
''
|
||||
];
|
||||
artifacts.paths = ["public"];
|
||||
};
|
||||
"pages" = {
|
||||
nix.enable = false;
|
||||
image = "alpine:latest";
|
||||
stage = "deploy";
|
||||
script = ["true"];
|
||||
artifacts.paths = ["public"];
|
||||
rules = [
|
||||
{
|
||||
"if" = "$CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH";
|
||||
}
|
||||
];
|
||||
};
|
||||
};
|
||||
};
|
||||
}
|
||||
27
nix/repo/devShells.nix
Normal file
27
nix/repo/devShells.nix
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
{inputs, ...}: let
|
||||
inherit (inputs) pkgs devshell treefmt;
|
||||
in {
|
||||
default = devshell.mkShell {
|
||||
packages = [
|
||||
pkgs.nil
|
||||
(treefmt.mkWrapper pkgs {
|
||||
programs = {
|
||||
alejandra.enable = true;
|
||||
deadnix.enable = true;
|
||||
statix.enable = true;
|
||||
mdformat.enable = true;
|
||||
yamlfmt.enable = true;
|
||||
};
|
||||
settings.formatter = {
|
||||
yamlfmt.excludes = ["templates/nix-gitlab-ci.yml"];
|
||||
mdformat.command = let
|
||||
pkg = pkgs.python3.withPackages (p: [
|
||||
p.mdformat
|
||||
p.mdformat-mkdocs
|
||||
]);
|
||||
in "${pkg}/bin/mdformat";
|
||||
};
|
||||
})
|
||||
];
|
||||
};
|
||||
}
|
||||
1
nix/repo/docs.nix
Normal file
1
nix/repo/docs.nix
Normal file
|
|
@ -0,0 +1 @@
|
|||
{}
|
||||
45
nix/repo/flake.lock
generated
Normal file
45
nix/repo/flake.lock
generated
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
{
|
||||
"nodes": {
|
||||
"devshell-lib": {
|
||||
"locked": {
|
||||
"dir": "lib",
|
||||
"lastModified": 1755673398,
|
||||
"narHash": "sha256-51MmR+Eo1+bKDd/Ss77wwTqi4yAR2xgmyCSEbKWSpj0=",
|
||||
"owner": "rensa-nix",
|
||||
"repo": "devshell",
|
||||
"rev": "e76bef387e8a4574f9b6d37b1a424e706491af08",
|
||||
"type": "gitlab"
|
||||
},
|
||||
"original": {
|
||||
"dir": "lib",
|
||||
"owner": "rensa-nix",
|
||||
"repo": "devshell",
|
||||
"type": "gitlab"
|
||||
}
|
||||
},
|
||||
"root": {
|
||||
"inputs": {
|
||||
"devshell-lib": "devshell-lib",
|
||||
"treefmt-nix": "treefmt-nix"
|
||||
}
|
||||
},
|
||||
"treefmt-nix": {
|
||||
"flake": false,
|
||||
"locked": {
|
||||
"lastModified": 1756662192,
|
||||
"narHash": "sha256-F1oFfV51AE259I85av+MAia221XwMHCOtZCMcZLK2Jk=",
|
||||
"owner": "numtide",
|
||||
"repo": "treefmt-nix",
|
||||
"rev": "1aabc6c05ccbcbf4a635fb7a90400e44282f61c4",
|
||||
"type": "github"
|
||||
},
|
||||
"original": {
|
||||
"owner": "numtide",
|
||||
"repo": "treefmt-nix",
|
||||
"type": "github"
|
||||
}
|
||||
}
|
||||
},
|
||||
"root": "root",
|
||||
"version": 7
|
||||
}
|
||||
16
nix/repo/flake.nix
Normal file
16
nix/repo/flake.nix
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
{
|
||||
inputs = {
|
||||
devshell-lib.url = "gitlab:rensa-nix/devshell?dir=lib";
|
||||
treefmt-nix = {
|
||||
url = "github:numtide/treefmt-nix";
|
||||
flake = false;
|
||||
};
|
||||
};
|
||||
outputs = i:
|
||||
i
|
||||
// {
|
||||
devshell = i.devshell-lib.lib {inherit (i.parent) pkgs;};
|
||||
cilib = import "${i.parent.self}/lib" {inherit (i.parent) pkgs;};
|
||||
treefmt = import i.treefmt-nix;
|
||||
};
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue