feat: rewrite how nixlets work

This commit is contained in:
TECHNOFAB 2024-05-08 17:09:17 +00:00
parent ba81fba6b2
commit 0cde19e51f
36 changed files with 494 additions and 125 deletions

View file

@ -3,11 +3,11 @@
lib,
...
} @ attrs:
with lib; {
with lib; rec {
mkValues = file: {rawValues, ...} @ args:
(lib.evalModules {
specialArgs = {
utils = import ./. attrs;
utils = import ./utils.nix attrs;
};
modules = [
file
@ -23,58 +23,103 @@ with lib; {
})
.config;
mkNestedOption = options:
mkOption {
type = types.submodule {
inherit options;
};
default = {};
};
# wraps mkNixletInner to allow passing either a path or an attrset
mkNixlet = arg:
mkNixletInner (
if (builtins.typeOf arg) == "set"
then arg
else
{path = arg;}
// (
if builtins.pathExists "${arg}/nixlet.nix"
then (import "${arg}/nixlet.nix")
else throw "Nixlet at '${arg}' does not contain nixlet.nix and mkNixlet was called with just a path"
)
);
mkNixlet = path: let
utils = import ./. attrs;
in
{
rawValues,
project,
...
} @ args: {
kubenix,
lib,
...
} @ attrs: let
values = utils.mkValues "${path}/values.nix" args;
in {
imports = [path];
# make values accessible from every imported file
_module.args = {inherit values;};
};
mkNixletInner = {
path,
name,
version ? null,
description ? "",
defaultProject ? null,
...
}: let
# TODO: just like with the values check the args here with the options system?
in {
inherit name version description path;
render = {
system,
project ? defaultProject,
overrides ? ({...}: {}),
values ? {},
}:
assert lib.assertMsg (project != null) "No default project set, please pass a project to the render method"; let
# every nixlet gets "nixlet" as arg with some useful data about itself
nixletArg = {
inherit name project version description;
};
in
(inputs.kubenix.evalModules.${system} {
module = {kubenix, ...}: {
imports = with kubenix.modules; [
k8s
helm
docker
files
({...}: let
finalValues = mkValues "${path}/values.nix" {
rawValues = values;
nixlet = nixletArg;
};
in {
imports = [path];
_module.args.nixlet =
{
values = finalValues;
}
// nixletArg;
})
overrides
];
kubenix.project = project;
};
})
.config
.kubernetes
.resultYAML;
};
renderNixlet = {
system,
project,
nixlet,
values ? {},
overrides ? {...}: {},
fetchNixlet = url: sha256: mkNixlet (builtins.fetchTarball {inherit url sha256;});
uploadNixletsToGitlab = {
pkgs,
projectId,
nixlets,
...
}:
(inputs.kubenix.evalModules.${system} {
module = {kubenix, ...}: {
imports = with kubenix.modules; [
k8s
helm
docker
files
(nixlet {
# all these args are available in values.nix
inherit project;
rawValues = values;
})
overrides
];
kubenix.project = project;
};
})
.config
.kubernetes
.resultYAML;
pkgs.writeShellScriptBin "nixlets-upload" (
''
if [[ -z "$AUTH_HEADER" ]]; then
echo "Must provide AUTH_HEADER environment variable!" 1>&2
exit 1
fi
''
+ lib.concatStringsSep "\n" (
builtins.map (nixlet:
with nixlet; ''
URL="https://gitlab.com/api/v4/projects/${projectId}/packages/generic/${name}/${version}/${name}.tar.gz"
if curl --output /dev/null --silent --head --fail --header "$AUTH_HEADER" $URL; then
echo "> Skipped ${name}@${version} because it already exists in the Package Registry"
else
echo "> Uploading new version ${name}@${version}"
${pkgs.gnutar}/bin/tar -czf /tmp/${name}.tar.gz --mode='u+rwX' -C ${path} .
${pkgs.curl}/bin/curl --header "$AUTH_HEADER" --upload-file "/tmp/${name}.tar.gz" "$URL"; echo;
${pkgs.coreutils}/bin/rm -f /tmp/${nixlet.name}.tar.gz
echo "> Finished ${name}@${version}, see above"
fi
'')
nixlets
)
);
}

102
lib/flake.lock generated Normal file
View file

@ -0,0 +1,102 @@
{
"nodes": {
"flake-compat": {
"flake": false,
"locked": {
"lastModified": 1673956053,
"narHash": "sha256-4gtG9iQuiKITOjNQQeQIpoIB6b16fm+504Ch3sNKLd8=",
"owner": "edolstra",
"repo": "flake-compat",
"rev": "35bb57c0c8d8b62bbfd284272c928ceb64ddbde9",
"type": "github"
},
"original": {
"owner": "edolstra",
"repo": "flake-compat",
"type": "github"
}
},
"kubenix": {
"inputs": {
"flake-compat": "flake-compat",
"nixpkgs": [
"nixpkgs"
],
"systems": "systems",
"treefmt": "treefmt"
},
"locked": {
"lastModified": 1709908607,
"narHash": "sha256-cG5PftryvQT5vTtRJGGsnfFgdobaUmD2klVVhzddFiU=",
"owner": "TECHNOFAB11",
"repo": "kubenix",
"rev": "ddfdd8d0903acff4a0a52b84e4395da1778dcc4c",
"type": "github"
},
"original": {
"owner": "TECHNOFAB11",
"repo": "kubenix",
"type": "github"
}
},
"nixpkgs": {
"locked": {
"lastModified": 1715037484,
"narHash": "sha256-OUt8xQFmBU96Hmm4T9tOWTu4oCswCzoVl+pxSq/kiFc=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "ad7efee13e0d216bf29992311536fce1d3eefbef",
"type": "github"
},
"original": {
"owner": "NixOS",
"ref": "nixpkgs-unstable",
"repo": "nixpkgs",
"type": "github"
}
},
"root": {
"inputs": {
"kubenix": "kubenix",
"nixpkgs": "nixpkgs"
}
},
"systems": {
"locked": {
"lastModified": 1681028828,
"narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=",
"owner": "nix-systems",
"repo": "default",
"rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e",
"type": "github"
},
"original": {
"id": "systems",
"type": "indirect"
}
},
"treefmt": {
"inputs": {
"nixpkgs": [
"kubenix",
"nixpkgs"
]
},
"locked": {
"lastModified": 1688026376,
"narHash": "sha256-qJmkr9BWDpqblk4E9/rCsAEl39y2n4Ycw6KRopvpUcY=",
"owner": "numtide",
"repo": "treefmt-nix",
"rev": "df3f32b0cc253dfc7009b7317e8f0e7ccd70b1cf",
"type": "github"
},
"original": {
"owner": "numtide",
"repo": "treefmt-nix",
"type": "github"
}
}
},
"root": "root",
"version": 7
}

22
lib/flake.nix Normal file
View file

@ -0,0 +1,22 @@
{
description = "Nixlets lib";
outputs = {
self,
nixpkgs,
kubenix,
...
} @ inputs:
import ./. {
inherit (nixpkgs) lib;
inherit inputs;
};
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
kubenix = {
url = "github:TECHNOFAB11/kubenix";
inputs.nixpkgs.follows = "nixpkgs";
};
};
}

10
lib/utils.nix Normal file
View file

@ -0,0 +1,10 @@
{lib, ...}:
with lib; {
mkNestedOption = options:
mkOption {
type = types.submodule {
inherit options;
};
default = {};
};
}