Skip to content

Commit

Permalink
internal: apply nixfmt
Browse files Browse the repository at this point in the history
Apply [official RFC nixfmt](NixOS/nixfmt#153).
  • Loading branch information
kai-tub committed Sep 23, 2024
1 parent 7d5b65b commit f5814cd
Show file tree
Hide file tree
Showing 5 changed files with 172 additions and 111 deletions.
77 changes: 47 additions & 30 deletions container.nix
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
{ quadletUtils }:
{ config, name, lib, ... }:
{
config,
name,
lib,
...
}:

with lib;

Expand Down Expand Up @@ -30,7 +35,12 @@ let
};

autoUpdate = quadletUtils.mkOption {
type = types.nullOr (types.enum [ "registry" "local" ]);
type = types.nullOr (
types.enum [
"registry"
"local"
]
);
default = null;
example = "registry";
description = "--label \"io.containers.autoupdate=...\"";
Expand All @@ -56,7 +66,9 @@ let
environments = quadletUtils.mkOption {
type = types.attrs;
default = { };
example = { foo = "bar"; };
example = {
foo = "bar";
};
description = "--env";
property = "Environment";
};
Expand Down Expand Up @@ -91,7 +103,7 @@ let
description = "--expose";
property = "ExposeHostPort";
};

group = quadletUtils.mkOption {
type = types.nullOr types.str;
default = null;
Expand Down Expand Up @@ -210,7 +222,7 @@ let
description = "--ip";
property = "IP";
};

ip6 = quadletUtils.mkOption {
type = types.nullOr types.str;
default = null;
Expand Down Expand Up @@ -242,7 +254,7 @@ let
description = "--mount";
property = "Mount";
};

networks = quadletUtils.mkOption {
type = types.listOf types.str;
default = [ ];
Expand Down Expand Up @@ -376,7 +388,9 @@ let
sysctl = quadletUtils.mkOption {
type = types.attrs;
default = { };
example = { name = "value"; };
example = {
name = "value";
};
description = "--sysctl";
property = "Sysctl";
};
Expand Down Expand Up @@ -441,7 +455,8 @@ let
Restart = "always";
TimeoutStartSec = 900;
};
in {
in
{
options = {
autoStart = mkOption {
type = types.bool;
Expand All @@ -452,7 +467,7 @@ in {
containerConfig = containerOpts;
unitConfig = mkOption {
type = types.attrs;
default = {};
default = { };
};
serviceConfig = mkOption {
type = types.attrs;
Expand All @@ -464,26 +479,28 @@ in {
_configText = mkOption { internal = true; };
};

config = let
configRelPath = "containers/systemd/${name}.container";
containerName = if config.containerConfig.name != null
then config.containerConfig.name
else name;
containerConfig = config.containerConfig // { name = containerName; };
unitConfig = {
Unit = {
Description = "Podman container ${name}";
} // config.unitConfig;
Install = {
WantedBy = if config.autoStart then [ "default.target" ] else [];
config =
let
configRelPath = "containers/systemd/${name}.container";
containerName = if config.containerConfig.name != null then config.containerConfig.name else name;
containerConfig = config.containerConfig // {
name = containerName;
};
Container = quadletUtils.configToProperties containerConfig containerOpts;
Service = serviceConfigDefault // config.serviceConfig;
};
unitConfigText = quadletUtils.unitConfigToText unitConfig;
in {
_configName = "${name}.container";
_unitName = "${name}.service";
_configText = quadletUtils.unitConfigToText unitConfig;
};
unitConfig = {
Unit = {
Description = "Podman container ${name}";
} // config.unitConfig;
Install = {
WantedBy = if config.autoStart then [ "default.target" ] else [ ];
};
Container = quadletUtils.configToProperties containerConfig containerOpts;
Service = serviceConfigDefault // config.serviceConfig;
};
unitConfigText = quadletUtils.unitConfigToText unitConfig;
in
{
_configName = "${name}.container";
_unitName = "${name}.service";
_configText = quadletUtils.unitConfigToText unitConfig;
};
}
14 changes: 8 additions & 6 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
};

outputs = { nixpkgs, ... }:
let
libUtils = import "${nixpkgs}/nixos/lib/utils.nix";
in {
nixosModules.quadlet = import ./nixos-module.nix { inherit libUtils; };
};
outputs =
{ nixpkgs, ... }:
let
libUtils = import "${nixpkgs}/nixos/lib/utils.nix";
in
{
nixosModules.quadlet = import ./nixos-module.nix { inherit libUtils; };
};
}
75 changes: 47 additions & 28 deletions network.nix
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
{ quadletUtils, pkgs }:
{ config, name, lib, ... }:
{
config,
name,
lib,
...
}:

with lib;

Expand All @@ -13,7 +18,13 @@ let
};

driver = quadletUtils.mkOption {
type = types.nullOr (types.enum [ "bridge" "macvlan" "ipvlan" ]);
type = types.nullOr (
types.enum [
"bridge"
"macvlan"
"ipvlan"
]
);
default = null;
example = "bridge";
description = "--driver";
Expand All @@ -36,7 +47,13 @@ let
};

ipamDriver = quadletUtils.mkOption {
type = types.nullOr (types.enum [ "host-local" "dhcp" "none" ]);
type = types.nullOr (
types.enum [
"host-local"
"dhcp"
"none"
]
);
default = null;
example = "dhcp";
description = "--ipam-driver";
Expand Down Expand Up @@ -98,7 +115,8 @@ let
property = "Subnet";
};
};
in {
in
{
options = {
autoStart = mkOption {
type = types.bool;
Expand All @@ -109,39 +127,40 @@ in {
networkConfig = networkOpts;
unitConfig = mkOption {
type = types.attrs;
default = {};
default = { };
};
serviceConfig = mkOption {
type = types.attrs;
default = {};
default = { };
};

_configName = mkOption { internal = true; };
_unitName = mkOption { internal = true; };
_configText = mkOption { internal = true; };
};

config = let
configRelPath = "containers/systemd/${name}.network";
networkName = if config.networkConfig.name != null
then config.networkConfig.name
else "systemd-${name}";
networkConfig = config.networkConfig;
unitConfig = {
Unit = {
Description = "Podman network ${name}";
} // config.unitConfig;
Install = {
WantedBy = if config.autoStart then [ "default.target" ] else [];
config =
let
configRelPath = "containers/systemd/${name}.network";
networkName =
if config.networkConfig.name != null then config.networkConfig.name else "systemd-${name}";
networkConfig = config.networkConfig;
unitConfig = {
Unit = {
Description = "Podman network ${name}";
} // config.unitConfig;
Install = {
WantedBy = if config.autoStart then [ "default.target" ] else [ ];
};
Network = quadletUtils.configToProperties networkConfig networkOpts;
Service = {
ExecStop = "${pkgs.podman}/bin/podman network rm ${networkName}";
} // config.serviceConfig;
};
Network = quadletUtils.configToProperties networkConfig networkOpts;
Service = {
ExecStop = "${pkgs.podman}/bin/podman network rm ${networkName}";
} // config.serviceConfig;
};
in {
_configName = "${name}.network";
_unitName = "${name}-network.service";
_configText = quadletUtils.unitConfigToText unitConfig;
};
in
{
_configName = "${name}.network";
_unitName = "${name}-network.service";
_configText = quadletUtils.unitConfigToText unitConfig;
};
}
84 changes: 48 additions & 36 deletions nixos-module.nix
Original file line number Diff line number Diff line change
@@ -1,22 +1,29 @@
{ libUtils }:
{ config, lib, pkgs, ... }@attrs:
{
config,
lib,
pkgs,
...
}@attrs:

with lib;

let
cfg = config.virtualisation.quadlet;
quadletUtils = import ./utils.nix {
inherit lib;
systemdLib = (libUtils {
inherit lib config pkgs;
}).systemdUtils.lib;
systemdLib =
(libUtils {
inherit lib config pkgs;
}).systemdUtils.lib;
};
# TODO: replace with lib.mergeAttrsList once stable.
mergeAttrsList = foldl mergeAttrs {};
mergeAttrsList = foldl mergeAttrs { };

containerOpts = types.submodule (import ./container.nix { inherit quadletUtils; } );
networkOpts = types.submodule (import ./network.nix { inherit quadletUtils pkgs; } );
in {
containerOpts = types.submodule (import ./container.nix { inherit quadletUtils; });
networkOpts = types.submodule (import ./network.nix { inherit quadletUtils pkgs; });
in
{
options = {
virtualisation.quadlet = {
containers = mkOption {
Expand All @@ -31,33 +38,38 @@ in {
};
};

config = let
allObjects = (attrValues cfg.containers) ++ (attrValues cfg.networks);
in {
virtualisation.podman.enable = true;
environment.etc = mergeAttrsList (
map (p: {
"containers/systemd/${p._configName}" = {
text = p._configText;
mode = "0600";
};
}) allObjects);
# The symlinks are not necessary for the services to be honored by systemd,
# but necessary for NixOS activation process to pick them up for updates.
systemd.packages = [
(pkgs.linkFarm "quadlet-service-symlinks" (
config =
let
allObjects = (attrValues cfg.containers) ++ (attrValues cfg.networks);
in
{
virtualisation.podman.enable = true;
environment.etc = mergeAttrsList (
map (p: {
name = "etc/systemd/system/${p._unitName}";
path = "/run/systemd/generator/${p._unitName}";
}) allObjects))
];
# Inject X-RestartIfChanged=${hash} for NixOS to detect changes.
systemd.units = mergeAttrsList (
map (p: {
${p._unitName} = {
overrideStrategy = "asDropin";
text = "[Unit]\nX-RestartIfChanged=${builtins.hashString "sha256" p._configText}";
};
}) allObjects);
};
"containers/systemd/${p._configName}" = {
text = p._configText;
mode = "0600";
};
}) allObjects
);
# The symlinks are not necessary for the services to be honored by systemd,
# but necessary for NixOS activation process to pick them up for updates.
systemd.packages = [
(pkgs.linkFarm "quadlet-service-symlinks" (
map (p: {
name = "etc/systemd/system/${p._unitName}";
path = "/run/systemd/generator/${p._unitName}";
}) allObjects
))
];
# Inject X-RestartIfChanged=${hash} for NixOS to detect changes.
systemd.units = mergeAttrsList (
map (p: {
${p._unitName} = {
overrideStrategy = "asDropin";
text = "[Unit]\nX-RestartIfChanged=${builtins.hashString "sha256" p._configText}";
};
}) allObjects
);
};
}
Loading

0 comments on commit f5814cd

Please sign in to comment.