1
0
Fork 0
mirror of https://git.sr.ht/~goorzhel/turboprop synced 2024-12-14 11:37:37 +00:00
turboprop/lib/builders.nix
Antonio Gurgel 70fae512d1 Refactor crisis
Two imperfections have come to bite me simultaneously:
- I wanted strict ordering of services but implemented it very sloppily.
- The flake builders represent implementation leakage. I want to present
  a clean interface to users, not "first, you must evaluate these
  twenty-eight variables".

So now I'm fixing too many things at once. Luckily it's hard to lose
things in Git.
2023-11-29 23:06:13 -08:00

68 lines
2.2 KiB
Nix

# All builders used to require a name and namespace, due to my previous
# (regrettable) decision to use the resultant derivations' names as
# path signifiers. No longer, because:
# 1. derivations are collected and directly mapped to target paths, and
# 2. the output derivation contains symbolic links to each sub-derivation
# for easy identification.
# Still, the pnames make derivations easy to identify in the Nix store
# at a glance, so I've kept them.
{
kubelib,
pkgs,
}: let
setNsOnObjects = namespace:
# Attrset is on LHS in case object sets its own namespace.
map (obj: pkgs.lib.attrsets.recursiveUpdate {metadata.namespace = namespace;} obj);
in rec {
derivation = {
name,
namespace,
src,
...
}:
pkgs.stdenv.mkDerivation {
pname = "copied-drv-${namespace}-${name}";
inherit (src) version;
inherit src;
phases = ["installPhase"];
installPhase = "cp -rv $src $out";
};
helmChart = {
name,
namespace,
...
} @ args: let
objs = kubelib.fromHelm args;
# Some charts lack diligence in setting `Release.Namespace`
# on the objects they create, necessitating a round-trip through
# yamlStream, which sets namespaces on all objects lacking one.
in
yamlStream {inherit name namespace objs;};
# Adapted from github:farcaller/nix-kube-generators ("kubelib").
yamlStream = {
name,
namespace,
objs,
...
}: let
# Some services may include extra objects outside of their charts.
# The following line removes the need for `namespace` to be explicitly
# passed into a service module.
# (Because all objects' metadata is of the ObjectMeta type, non-namespaced
# objects can have `metadata.namespace` set, too. It will just be ignored
# at creation time in the Kubernetes cluster.)
namespacedObjs = setNsOnObjects namespace objs;
in
pkgs.stdenv.mkDerivation {
name = "yaml-stream-${namespace}-${name}";
yamlText = pkgs.lib.strings.concatStringsSep "\n---\n" (map builtins.toJSON namespacedObjs);
passAsFile = "yamlText";
phases = ["installPhase"];
installPhase = "${pkgs.yq-go}/bin/yq -P -M $yamlTextPath > $out";
};
}