move container generation to its own file

Signed-off-by: ahuston-0 <aliceghuston@gmail.com>
This commit is contained in:
ahuston-0 2024-06-23 21:21:59 -04:00
parent f6bda933ea
commit 6e75e84e8b
No known key found for this signature in database
GPG Key ID: 1FACF4075E3212F7
2 changed files with 44 additions and 71 deletions

43
lib/container-utils.nix Normal file
View File

@ -0,0 +1,43 @@
{ lib, ... }:
{
# Given a attrset of images and a function which generates an image spec,
# generates a set of containers (although this could in theory be used for
# other things... I'd like to see people try)
#
# container set must be in the below format
# { container-name = {image = "image-uri"; scale = n;}; }
# where image-uri gets passed in to the container-spec function as a custom
# parameter, and scale is an integer that generates the containers
#
# container-spec must be a function which accepts two parameter (the
# container name and image name) and ideally returns an oci-compliant
# container.
#
# args:
# containers: an AttrSet which specifies the imageUri and scale of each
# container
# container-spec: a function which produces an oci-compliant container spec
#
# type:
# AttrSet -> (String -> AttrSet -> AttrSet) -> AttrSet
createTemplatedContainers =
containers: container-spec:
builtins.listToAttrs (
lib.flatten (
lib.mapAttrsToList (
name: value:
(map (
num:
let
container-name = "${name}-${toString num}";
in
{
name = container-name;
value = container-spec container-name value.image;
}
) (lib.lists.range 1 value.scale))
) containers
)
);
}

View File

@ -3,6 +3,7 @@
# create rad-dev namespace for lib # create rad-dev namespace for lib
rad-dev = rec { rad-dev = rec {
systems = import ./systems.nix { inherit lib; }; systems = import ./systems.nix { inherit lib; };
container-utils = import ./container-utils.nix { inherit lib; };
# any(), but checks if any value in the list is true # any(), but checks if any value in the list is true
# #
@ -57,76 +58,5 @@
# fileList :: Path -> String -> [Path] # fileList :: Path -> String -> [Path]
fileList = dir: map (file: dir + "/${file}") (ls dir); fileList = dir: map (file: dir + "/${file}") (ls dir);
# Given a attrset of images and a function which generates an image spec,
# generates a set of containers (although this could in theory be used for
# other things... I'd like to see people try)
#
# container set must be in the below format
# { container-name = {image = "image-uri"; scale = n;}; }
# where image-uri gets passed in to the container-spec function as a custom
# parameter, and scale is an integer that generates the containers
#
# container-spec must be a function which accepts two parameter (the
# container name and image name) and ideally returns an oci-compliant
# container.
#
# args:
# containers: an AttrSet which specifies the imageUri and scale of each
# container
# container-spec: a function which produces an oci-compliant container spec
#
# type:
# AttrSet -> (String -> AttrSet -> AttrSet) -> AttrSet
createTemplatedContainers =
containers: container-spec:
builtins.listToAttrs (
lib.flatten (
lib.mapAttrsToList (
name: value:
(map (
num:
let
container-name = "${name}-${toString num}";
in
{
name = container-name;
value = container-spec container-name value.image;
}
) (lib.lists.range 1 value.scale))
) containers
)
);
# Converts an integer into a string
#
# Given that trying to do this the way you'd do it in languages like python
# ("str" + int) causes the program to error out, I assume this is some sort
# of cardinal sin of nix. However, I want templated docker containers so
# here we are.
#
# Fun fact: if you want to parse a negative number (I know, scary right?),
# you first need to surround it in parentheses (ie. parseInt (-1000)) as nix
# interprets negative numbers as a function and will think you're trying to
# do some really weird function application magic if you try to call this
# normally (ie. parseInt -1000).
#
# args:
# num: an integer to convert
#
# type:
# parseInt :: Integer -> String
parseInt =
num:
let
digits = "0123456789";
mod = num: (lib.trivial.mod num 10);
in
if num > 9 then
((parseInt (builtins.div num 10)) + (lib.substring (mod num) 1 digits))
else if num < 0 then
"-" + (parseInt (builtins.mul num (-1)))
else
lib.substring (mod num) 1 digits;
}; };
} }