Hello there ! I’m trying to refac an old server of mine using containers instead of running everything natively. However, when checking the machine flake, I get this error:
warning: Git tree '/home/kheldae/Documents/Bodaway' is dirty
error:
… while checking flake output 'nixosConfigurations'
at /home/kheldae/Documents/Bodaway/flake.nix:36:7:
35| {
36| nixosConfigurations =
| ^
37| let
… while checking the NixOS configuration 'nixosConfigurations."Bodaway:Kvothe"'
at «github:NixOS/nixpkgs/7dcdd87373d72e7a2173e4db02e20d3d7f495cfd?narHash=sha256-tItrb9M8eYA6WhFgmT9tpGKueri3%2BwRuVA952o/2xSU%3D»/lib/attrsets.nix:987:47:
986| */
987| nameValuePair = name: value: { inherit name value; };
| ^
988|
… while evaluating the option `containers.Kvothe.config':
(stack trace truncated; use '--show-trace' to show the full, detailed trace)
error: The option `containers.Kvothe.systemd.enable' does not exist. Definition values:
- In `/nix/store/2xl4y8v0mpkqbvxdra2c0ci02w8mczyy-source/nixos/modules/virtualisation/nixos-containers.nix':
{
_type = "if";
condition = false;
content = true;
}
Did you mean `containers.Kvothe.systemd.paths', `containers.Kvothe.systemd.repart' or `containers.Kvothe.systemd.units'?
Does anybody has any idea why it’s thrown ? And this is the Kvothe configuration :
{ pkgs, container, ... }:
{
# MariaDB configuration
config = container {
addr = "10.42.0.32";
# exposed ports
fwds = [
{
containerPort = 3306;
hostPort = 3306;
}
];
# volumes
mounts = {
"/var/lib/mysql" = {
hostPath = "/opt/mysql_db";
isReadOnly = false;
};
};
# core db service
services.mysql = {
enable = true;
package = pkgs.mariadb;
configFile = builtins.toFile "my.cnf" (builtins.readFile ./res/my.cnf);
};
# tweak launch timers for database init
systemd.services."mysql.service".serviceConfig = {
TimeoutStartSec = 9999; # as much time as we can
};
};
}
I tried for a bit to write a coherent response, but really it’s impossible to tell with this amount of information about your config. (That container helper function in particular is doing a lot of heavy-lifting for this to be a remotely workable container config, and it’s completely a black box.) Could you please post a link to a public repo with your config, or if that absolutely isn’t possible, show substantially more of the related files?
The best advice I can give without that is that you might be putting your config at the top-level of the module, instead of under the config option? The top-level is for describing meta-properties about the container (e.g. containers.Kvothe.autoStart, while the configuration which runs in the container should use the config (or path or flake) option instead.
{ pkgs, config, ... }@host:
{
services ? { },
systemd ? { }, # current container's systemd configuration
nixpkgs ? host.pkgs, # ensure packages are known
netJail ? false, # Lock container outside of the web
addr,
fwds ? [ ], # exposed ports by the container
mounts ? { }, # volumes required by the container
addOCI ? false, # FUSE required
extraConfig ? { }, # extra configuration options (per container)
}:
let
# networking
ip4Net = if netJail then "10.84.0.0/16" else "10.42.0.0/16";
ip4Gateway = if netJail then "10.84.0.1" else "10.42.0.1";
# address calculus
assertJail = (!netJail) -> (nixpkgs.lib.hasPrefix "10.42" addr);
warnJail = (netJail) -> (nixpkgs.lib.hasPrefix "10.42" addr);
address =
if assertJail then
nixpkgs.lib.warnIfNot warnJail "Address ${addr} in jailed network. Internet is disabled."
"${addr}/16"
else
throw "Address ${addr} not in known network";
# determine if we need to tweak the filesystem
isBindfsNeeded = builtins.foldl' (l: r: l || builtins.hasAttr "user" r) false (
nixpkgs.lib.attrValues mounts
);
# fetch necessary mount information for user mounts
mountsWithUsers = nixpkgs.lib.filterAttrs (k: v: builtins.hasAttr "user" v) mounts;
# extra logic
devIf =
condi: device:
nixpkgs.lib.mkIf condi {
modifier = "rwm";
node = "/dev/${device}";
};
bindIf =
condi: path: hostPath:
if condi then
{
"${path}" = {
hostPath = hostPath;
isReadOnly = false;
};
}
else
{ };
in
{
config =
with nixpkgs;
{
# load minimal services setup and disable network resolver
services = services // {
resolved.enable = lib.mkForce false;
};
systemd =
systemd
# tweak filesystem for the container mount
// lib.mkIf isBindfsNeeded {
services."bindfs-remount" = {
before = [ "multi-user.target" ];
wantedBy = [ "multi-user.target" ];
path = [ pkgs.bindfs ];
# custom launcher script
script =
builtins.foldl' (l: r: "${1}\n${r}") ""
<| lib.attrValues
<| builtins.mapAttrs (
k: v: "bindfs --force-user-${v.user} --force-group-${v.group or "0"} ${k} ${k}"
) mountsWithUsers;
};
enable = true;
};
# network settings
networking = {
# networking resolution tools
nameservers = [
"192.168.1.1"
"1.1.1.1"
];
useHostResolvConf = lib.mkForce false;
# gateways
defaultGateway = ip4Gateway;
# firewall config
firewall.extraCommands = lib.mkIf (!netJail) ''
iptables -A INPUT -s ${ip4Net} -j ACCEPT
'';
};
# adding unfree software to nix build
nixpkgs.config.allowUnfree = true;
system.stateVersion = host.config.system.stateVersion;
# virtualisation toolkit
virtualisation = {
containers.enable = true;
# remove docker
docker.enable = lib.mkForce false;
# set our engine of choice
podman = lib.mkIf addOCI {
enable = true;
dockerCompat = true;
docker.socket.enable = true;
defaultNetwork.setting.dns_enabled = true;
};
};
}
// extraConfig;
# ensure we have a copy of the current nix-store
nixpkgs = nixpkgs.path;
# Startup settings
autoStart = true;
ephemeral = true; # ensuring we launch it in rootless mode
timeoutStartSec = "10min"; # because databases takes roughly 5min
# Network settings
privateNetwork = true;
hostBridge = if netJail then "br1" else "br0";
localAddress = address;
forwardPorts = fwds;
}
this is the current boilerplate I’m using if that can help. The point is for every call of container, it calls this file and the rest of the flake doesn’t really know how it’s wired inside, it just have to wire everything together.