Skip to content

Commit

Permalink
Add incus container
Browse files Browse the repository at this point in the history
  • Loading branch information
srid committed Feb 1, 2025
1 parent 11dc14d commit a95f675
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 12 deletions.
20 changes: 20 additions & 0 deletions configurations/nixos/public-container/configuration.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{ flake, pkgs, ... }:

let
inherit (flake) inputs;
inherit (inputs) self;
in
{
imports = [
"${inputs.nixpkgs}/nixos/modules/virtualisation/lxc-container.nix"
(self + /modules/nixos/shared/primary-as-admin.nix)
];

nixpkgs.hostPlatform = "x86_64-linux";
networking.firewall.enable = true;

# Workaround the annoying `Failed to start Network Manager Wait Online` error on switch.
# https://github.com/NixOS/nixpkgs/issues/180175
systemd.services.NetworkManager-wait-online.enable = false;
system.stateVersion = "24.11";
}
19 changes: 19 additions & 0 deletions configurations/nixos/public-container/default.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{ pkgs, ... }:
{
imports = [
./configuration.nix
];


# Hello world service
services.nginx = {
enable = true;
# Return "Hello World" on / request
virtualHosts."_" = {
root = "${pkgs.writeTextDir "index.html" "Hello World"}";
};
};
networking.firewall = {
allowedTCPPorts = [ 80 ];
};
}
41 changes: 29 additions & 12 deletions modules/flake-parts/incus-image.nix
Original file line number Diff line number Diff line change
@@ -1,18 +1,35 @@
{ inputs, ... }: {
perSystem = { pkgs, ... }: {
apps.incus-image-import.program = pkgs.writeShellApplication {
name = "incus-image-import";
text = ''
NAME=$1
perSystem = { pkgs, system, ... }: {
apps = {
incus-image-vm-import.program = pkgs.writeShellApplication {
name = "incus-image-vm-import";
text = ''
NAME=$1
echo "Building image ... "
METADATA=$(nix build --no-link --print-out-paths ${inputs.self}#nixosConfigurations."$NAME".config.system.build.metadata)/tarball/
IMG=$(nix build --no-link --print-out-paths ${inputs.self}#nixosConfigurations."$NAME".config.system.build.qemuImage)/nixos.qcow2
echo "Building image ... "
METADATA=$(nix build --no-link --print-out-paths ${inputs.self}#nixosConfigurations."$NAME".config.system.build.metadata)/tarball/
IMG=$(nix build --no-link --print-out-paths ${inputs.self}#nixosConfigurations."$NAME".config.system.build.qemuImage)/nixos.qcow2
echo "Importing ... "
set -x
sudo incus image import --alias srid/"$NAME" "$METADATA"/*.tar.xz "$IMG"
'';
echo "Importing ... "
set -x
sudo incus image import --alias srid/"$NAME" "$METADATA"/*.tar.xz "$IMG"
'';
};

incus-image-container-import.program = pkgs.writeShellApplication {
name = "incus-image-container-import";
text = ''
NAME=$1
echo "Building image ... "
METADATA=$(nix build --no-link --print-out-paths ${inputs.self}#nixosConfigurations."$NAME".config.system.build.metadata)/tarball/
IMG=$(nix build --no-link --print-out-paths ${inputs.self}#nixosConfigurations."$NAME".config.system.build.squashfs)/nixos-lxc-image-${system}.squashfs
echo "Importing ... "
set -x
sudo incus image import --alias srid/"$NAME" "$METADATA"/*.tar.xz "$IMG"
'';
};
};
};
}

0 comments on commit a95f675

Please sign in to comment.