Skip to content

Instantly share code, notes, and snippets.

@c0deaddict
Created January 1, 2022 17:02
Show Gist options
  • Save c0deaddict/1ba67012d857ef541e707e8c8cba163c to your computer and use it in GitHub Desktop.
Save c0deaddict/1ba67012d857ef541e707e8c8cba163c to your computer and use it in GitHub Desktop.
Declarative LXD config
{ lib, config, pkgs, ... }:
with lib;
let
cfg = config.mine.lxc;
format = pkgs.formats.json { };
preseedFile = format.generate "preseed.yaml" cfg.preseed;
in {
options.mine.lxc = {
enable = mkEnableOption "lxc";
zfsPool = mkOption {
type = types.str;
example = "main/local/lxc";
};
preseed = mkOption {
default = { };
type = format.type;
example = literalExpression ''
{
config = {
"images.auto_update_interval" = 15;
};
};
'';
description = ''
LXD preseed extra configuration. See
<link xlink:href="https://linuxcontainers.org/lxd/docs/master/preseed/">
the documentation</link> for a list of options.
'';
};
};
config = mkIf cfg.enable {
mine.lxc.preseed = {
networks = [{
name = "lxdbr0";
type = "bridge";
config = {
"ipv4.address" = "10.200.0.1/24";
"ipv6.address" = "fd42::1/64";
};
}];
storage_pools = [{
name = "default";
driver = "zfs";
config.source = cfg.zfsPool;
}];
profiles = [{
name = "default";
devices.eth0 = {
name = "eth0";
network = "lxdbr0";
type = "nic";
};
devices.root = {
path = "/";
pool = "default";
type = "disk";
};
}];
};
virtualisation.lxd = {
enable = true;
zfsSupport = true;
recommendedSysctlSettings = true;
};
virtualisation.lxc = {
enable = true;
lxcfs.enable = true;
defaultConfig = ''
lxc.include = ${pkgs.lxcfs}/share/lxc/config/common.conf.d/00-lxcfs.conf
'';
};
systemd.services."lxd-preseed" = {
description = "Preseed LXD";
wantedBy = [ "multi-user.target" ];
requires = [ "lxd.socket" ];
serviceConfig = {
Type = "oneshot";
ExecStart = pkgs.writers.writeDash "preseed" ''
cat ${preseedFile} | ${pkgs.lxd}/bin/lxd init --preseed
'';
};
};
};
}
@aolasz
Copy link

aolasz commented May 6, 2022

Should I just import this file after editing?
I tried to import it but it had no effect; lxc/lxd was not installed after rebuilding the system.

@aolasz
Copy link

aolasz commented May 6, 2022

Ok, got it. I forgot the
mine.lxc.enable = true;
line.

Thanks for this module!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment