1
0
Fork 0
mirror of https://git.sr.ht/~goorzhel/turboprop synced 2024-12-14 11:37:37 +00:00

Factor out (and document exhaustively) flake-builders

This commit is contained in:
Antonio Gurgel 2023-11-18 23:09:40 -08:00
parent cc159c6ff9
commit 2c213d6222
4 changed files with 77 additions and 38 deletions

View file

@ -39,46 +39,21 @@
};
kubelib = nix-kube-generators.lib {inherit pkgs;};
lib = import ./lib {inherit charts kubelib pkgs;};
flake-builders = import ./lib/flake-builders.nix {inherit pkgs lib;};
# TODO: Time to factor out bi-level mapAttrs calls...
downloadCharts = reponame: charts:
builtins.mapAttrs
(chartname: chartspec: kubelib.downloadHelmChart chartspec)
charts;
charts = builtins.mapAttrs downloadCharts self.repos;
buildReleases = namespace: releases:
builtins.mapAttrs
(name: module: (
let
release = module {inherit lib;};
in
release.builder
(release.args // {inherit name namespace;})
))
releases;
releases = builtins.mapAttrs buildReleases self.releaseData;
buildExtras = namespace: releases:
builtins.mapAttrs
(name: module: (
let
release = module {inherit lib;};
in
if release ? extraObjects
then
lib.builders.buildYAMLStream {
inherit name namespace;
objs = release.extraObjects;
}
else {}
))
releases;
extras = builtins.mapAttrs buildExtras self.releaseData;
charts = flake-builders.fetchCharts self.repos;
releases = flake-builders.buildReleases self.releaseData;
extras = flake-builders.buildExtras self.releaseData;
in {
packages = {
inherit charts releases extras;
inherit charts;
inherit releases extras; # useful for debugging; will go to own flake eventually
inherit (self) releaseData;
# Each of the leaves of the `releases` and `extras` attrsets
# is a derivation (explained better in `lib/flake-builders.nix`).
# Here, they are gathered into one mega-derivation, with
# Kustomizations at each level for usage with `kubectl apply -k $path`.
default = pkgs.stdenv.mkDerivation {
pname = "kubeflake";
version = "0.0.1";

View file

@ -5,7 +5,7 @@
}: rec {
appTemplate = import ./app-template.nix {inherit charts kubelib pkgs vars;};
builders = import ./builders.nix {inherit kubelib pkgs;};
fetchers = import ./fetchers.nix {inherit pkgs;};
fetchers = import ./fetchers.nix {inherit kubelib pkgs;};
vars = import ./vars.nix;
mkExternalServiceEntry = name: hosts: {

View file

@ -1,4 +1,7 @@
{pkgs}: {
{
kubelib,
pkgs,
}: {
remoteYAMLFile = {
version,
url,
@ -14,6 +17,8 @@
installPhase = "cp -v $src $out";
};
helmChart = kubelib.downloadHelmChart;
gitChart = {
name,
version,

59
lib/flake-builders.nix Normal file
View file

@ -0,0 +1,59 @@
# These functions take bi-level attrsets generated by rakeLeaves.
# Each leaf of the input attrset represents an unbuilt derivation,
# its type being:
# - for chart data, `{repo, chart, version, hash}`, or
# - for release data:
# `module :: {lib} -> {builder, args, extraObjects}`.
# In other words, these builders transform these:
# - repos.bjw-s.app-template = {repo, chart, version, hash}
# - releaseData.svc.breezewiki = {lib}: {builder, args, extraObjects}
# into:
# - charts.bjw-s.app-template = <derivation>
# - releases.svc.breezewiki = <derivation>
{
pkgs,
lib,
...
}:
with builtins; {
fetchCharts =
mapAttrs
(reponame: charts:
mapAttrs
(chartname: chartspec: lib.fetchers.helmChart chartspec)
charts);
buildReleases =
mapAttrs
(namespace: releases:
mapAttrs
(name: module: (
let
release = module {inherit lib;};
in
release.builder
(release.args // {inherit name namespace;})
# By injecting `name` and `namespace` here, I remove
# the need to specify it for every release in the flake;
# the directory structure can speak for me instead.
))
releases);
buildExtras =
mapAttrs
(namespace: releases:
mapAttrs
(name: module: (
let
release = module {inherit lib;};
in
if release ? extraObjects
then
lib.builders.buildYAMLStream {
inherit name namespace;
objs = release.extraObjects;
}
else {}
))
releases);
}