refactor(arion): setup options instead of importing attrsets
All checks were successful
Discord / discord commits (push) Has been skipped

This commit is contained in:
matt1432 2024-01-22 15:50:05 -05:00
parent f03165caa5
commit 2b14ecc1b8
6 changed files with 160 additions and 153 deletions

View file

@ -1,127 +1,20 @@
{ {...}: let
arion,
config,
lib,
pkgs,
self,
...
} @ inputs: let
inherit (builtins) match;
inherit
(lib)
filter
hasSuffix
filesystem
hasAttr
mapAttrs
filterAttrs
listToAttrs
elemAt
removePrefix
optionalAttrs
mkForce
;
inherit (config.vars) mainUser hostName;
configPath = "/var/lib/arion"; configPath = "/var/lib/arion";
in { in {
imports = [arion.nixosModules.arion]; imports = [
../../../../modules/arion.nix
users.extraUsers.${mainUser}.extraGroups = ["docker"]; ./forgejo/compose.nix
./music/jbots/compose.nix
environment.systemPackages = with pkgs; [
nix-prefetch-docker
(writeShellScriptBin "updateImages" ''
pull() {
FILE="$1"
IMAGE=$(sed -n 's/.*imageName = "\([^"]*\).*/\1/p' "$FILE")
TAG=$(sed -n 's/.*finalImageTag = "\([^"]*\).*/\1/p' "$FILE")
echo "$IMAGE $TAG"
PREFETCH=$(nix-prefetch-docker "$IMAGE" "$TAG")
echo -e "pkgs:\npkgs.dockerTools.pullImage $PREFETCH" > "$FILE"
}
DIR="$1"
if [ -z "$DIR" ]; then
DIR="."
fi
IFS=$'\n'
for i in $(find "$DIR"/images -type f); do
pull "$i"
done
unset IFS
'')
]; ];
arion = {
enable = true;
rwDataDir = configPath;
};
services.borgbackup.configs.arion = { services.borgbackup.configs.arion = {
paths = [configPath]; paths = [configPath];
exclude = ["**/lineageos*"]; exclude = ["**/lineageos*"];
}; };
virtualisation = {
docker = {
enable = true;
storageDriver = "btrfs";
};
arion = {
backend = "docker";
projects = let
basePath = "${self}/devices/${hostName}/modules/arion";
composeFiles =
filter (n: hasSuffix "compose.nix" (toString n))
(filesystem.listFilesRecursive basePath);
projects = filterAttrs (n: v: v.enabled or true) (listToAttrs (map (p: {
name = elemAt (match ".*\/(.*)\/compose\.nix" (toString p)) 0;
value = import p (inputs
// {
importImage = file: pkgs.callPackage file pkgs;
rwPath =
configPath
+ "/"
+ removePrefix basePath "${dirOf p}";
});
})
composeFiles));
in
mapAttrs (n: v: {
# https://docs.hercules-ci.com/arion/options
settings = {
enableDefaultNetwork = v.enableDefaultNetwork or true;
networks =
optionalAttrs (hasAttr "networks" v)
v.networks;
services =
mapAttrs (n': v': {
# https://github.com/hercules-ci/arion/issues/169#issuecomment-1301370634
build.image =
optionalAttrs (hasAttr "hostImage" v')
(mkForce v'.hostImage);
image =
optionalAttrs (hasAttr "customImage" v')
v'.customImage;
service =
filterAttrs
(n: v: n != "customImage" && n != "hostImage")
v';
})
v.services;
};
})
projects;
};
};
} }

View file

@ -1,15 +1,12 @@
{ {config, ...}: let
config, inherit (config.sops) secrets;
rwPath, inherit (config.arion) rwDataDir;
importImage,
... rwPath = rwDataDir + "/forgejo";
}: let
secrets = config.sops.secrets;
in { in {
services = { arion.projects."forgejo" = {
"forgejo" = { "forgejo" = {
container_name = "forgejo"; image = ./images/forgejo.nix;
hostImage = importImage ./images/forgejo.nix;
ports = [ ports = [
# Redirect WAN port 22 to this port # Redirect WAN port 22 to this port
@ -42,8 +39,7 @@ in {
}; };
"forgejo-db" = { "forgejo-db" = {
container_name = "forgejo-db"; image = ./images/postgres.nix;
hostImage = importImage ./images/postgres.nix;
restart = "always"; restart = "always";
@ -52,9 +48,8 @@ in {
volumes = ["${rwPath}/db:/var/lib/postgresql/data"]; volumes = ["${rwPath}/db:/var/lib/postgresql/data"];
}; };
"runner" = { "act_runner" = {
container_name = "act_runner"; image = ./images/act_runner.nix;
hostImage = importImage ./images/act_runner.nix;
privileged = true; privileged = true;
restart = "always"; restart = "always";

View file

@ -1,8 +1,8 @@
pkgs: pkgs:
pkgs.dockerTools.pullImage { pkgs.dockerTools.pullImage {
imageName = "vegardit/gitea-act-runner"; imageName = "vegardit/gitea-act-runner";
imageDigest = "sha256:3365bb367c7f45fd00a0bc229effa616bc167fd8215a558ba1123a8f05dfaf9a"; imageDigest = "sha256:10965cf2fcd3cc146f2bd54e0234d55a499f1dedc3a1f2edd7fb5736b4921904";
sha256 = "1q3gr55z08prf0531889sa4i6g07mlpnw7lsjb7vz3yn0jmh3jpz"; sha256 = "0pi09zz7agm33jbd41zi94lqfxmq7q64z2lfjlmdp9aq1q254zxw";
finalImageName = "vegardit/gitea-act-runner"; finalImageName = "vegardit/gitea-act-runner";
finalImageTag = "dind-latest"; finalImageTag = "dind-latest";
} }

View file

@ -1,16 +1,13 @@
{ {config, ...}: let
config, inherit (config.sops) secrets;
rwPath, inherit (config.arion) rwDataDir;
...
}: let rwPath = rwDataDir + "/lineageos";
secrets = config.sops.secrets;
in { in {
# FIXME: crashes when building # FIXME: crashes when building
# TODO: make sure it works with latest arion custom module
# This should only be ran when an update is needed arion.projects."lineageos" = {
enabled = false;
services = {
"builder" = { "builder" = {
image = "lineageos4microg/docker-lineage-cicd"; image = "lineageos4microg/docker-lineage-cicd";
container_name = "lineage_builder"; container_name = "lineage_builder";
@ -54,7 +51,6 @@ in {
"caddy" = { "caddy" = {
image = "quay.io/slothcroissant/caddy-cloudflaredns:latest"; image = "quay.io/slothcroissant/caddy-cloudflaredns:latest";
container_name = "caddy";
ports = [ ports = [
"80:80" "80:80"

View file

@ -1,12 +1,12 @@
{ {config, ...}: let
rwPath, inherit (config.arion) rwDataDir;
importImage,
... rwPath = rwDataDir + "/music/jbots";
}: { in {
services = { arion.projects."jbots" = {
"musicbot_be" = { "musicbot_be" = {
container_name = "benis"; container_name = "benis";
hostImage = importImage ./images/jmusicbot.nix; image = ./images/jmusicbot.nix;
restart = "always"; restart = "always";
volumes = [ volumes = [
@ -17,7 +17,7 @@
"musicbot_br" = { "musicbot_br" = {
container_name = "bruh"; container_name = "bruh";
hostImage = importImage ./images/jmusicbot.nix; image = ./images/jmusicbot.nix;
restart = "always"; restart = "always";
volumes = [ volumes = [

123
modules/arion.nix Normal file
View file

@ -0,0 +1,123 @@
{
arion,
config,
lib,
pkgs,
...
}: let
inherit
(lib)
filterAttrs
hasAttr
mapAttrs
mkEnableOption
mkForce
mkIf
mkOption
optionalAttrs
types
;
inherit (config.vars) mainUser;
cfg = config.arion;
in {
imports = [arion.nixosModules.arion];
options.arion = {
enable = mkEnableOption (lib.mdDoc "My custom arion config layer module");
rwDataDir = mkOption {
default = "/var/lib/arion";
type = types.str;
description = lib.mdDoc ''
Directory to place persistent data in
'';
};
projects = mkOption {
default = {};
description = lib.mdDoc ''
Declarative specification of docker-compose in nix.
'';
type = types.attrs;
};
};
config = mkIf cfg.enable {
users.extraUsers.${mainUser}.extraGroups = ["docker"];
virtualisation = {
docker = {
enable = true;
storageDriver = "btrfs";
};
arion = {
backend = "docker";
projects =
mapAttrs (n: v: {
# https://docs.hercules-ci.com/arion/options
settings = {
enableDefaultNetwork = v.enableDefaultNetwork or true;
networks =
optionalAttrs (hasAttr "networks" v)
v.networks;
services =
mapAttrs (n': v': {
# https://github.com/hercules-ci/arion/issues/169#issuecomment-1301370634
build.image = let
importImage = file: pkgs.callPackage file pkgs;
in
mkForce (importImage v'.image);
service =
(filterAttrs
(attrName: _: attrName != "image")
v')
# By default set the container_name to the attrset's name
// (optionalAttrs (! hasAttr "container_name" v') {
container_name = n';
});
})
v;
};
})
cfg.projects;
};
};
# Script for updating the images of all images of a compose.nix file
environment.systemPackages = with pkgs; [
(writeShellApplication {
name = "updateImages";
runtimeInputs = [
(writeShellApplication {
name = "pullImage";
runtimeInputs = [nix-prefetch-docker];
text = ''
FILE="$1"
IMAGE=$(sed -n 's/.*imageName = "\([^"]*\).*/\1/p' "$FILE")
TAG=$(sed -n 's/.*finalImageTag = "\([^"]*\).*/\1/p' "$FILE")
echo "$IMAGE $TAG"
PREFETCH=$(nix-prefetch-docker "$IMAGE" "$TAG")
echo -e "pkgs:\npkgs.dockerTools.pullImage $PREFETCH" > "$FILE"
'';
})
];
text = ''
DIR=''${1:-"."}
find "$DIR"/images -type f -exec pullImage {} \;
'';
})
];
};
}