-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflake.nix
More file actions
100 lines (89 loc) · 3.21 KB
/
flake.nix
File metadata and controls
100 lines (89 loc) · 3.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
{
description = "Qubix: declarative Hyper-V appliance factory for NixOS VMs";
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-25.11";
nixos-generators = {
url = "github:nix-community/nixos-generators";
inputs.nixpkgs.follows = "nixpkgs";
};
};
outputs = { self, nixpkgs, nixos-generators }:
let
system = "x86_64-linux";
pkgs = import nixpkgs {
inherit system;
config.allowUnfreePredicate = pkg:
builtins.elem (nixpkgs.lib.getName pkg) [
"spotify"
];
};
# Evaluate a machine's NixOS config and return its config object.
# The Hyper-V manifest is derived from evaluated machine configs so that
# settings like hostName and static networking have one source of truth:
# the machine file. No more keeping two files in sync by hand.
evalMachine = modules: (nixpkgs.lib.nixosSystem {
inherit system pkgs;
modules = modules;
specialArgs = { inherit self; };
}).config;
# Extract network-related manifest fields from an evaluated NixOS config.
# Returns an empty attrset when no static IP is configured (DHCP mode).
mkNetworkFields = cfg:
let net = cfg.qubix.network;
in nixpkgs.lib.optionalAttrs (net.staticIp != null) {
staticIp = net.staticIp;
gatewayIp = net.gateway;
# Derive subnet from staticIp + prefixLength (correct for /17-/24).
natSwitchSubnet =
let parts = nixpkgs.lib.splitString "." net.staticIp;
in "${builtins.elemAt parts 0}.${builtins.elemAt parts 1}"
+ ".${builtins.elemAt parts 2}.0/${toString net.prefixLength}";
};
spotiboxCfg = evalMachine [ ./machines/spotibox.nix ];
qubixManifest = {
spotibox = {
package = "spotibox-vhdx";
# Derived from the machine config — only one place to change.
vmName = "qubix-${spotiboxCfg.networking.hostName}";
hostName = spotiboxCfg.networking.hostName;
# Used in DHCP mode; ignored when staticIp is present.
switchName = "Default Switch";
cpuCount = 2;
memoryStartupBytes = 4294967296;
maxMemoryBytes = 6442450944;
vmRoot = "C:\\HyperV\\Qubix";
wslDistro = "NixOS";
# Network fields are present only when qubix.network.staticIp is set
# in the machine file. Nothing to change here when toggling static IP.
} // mkNetworkFields spotiboxCfg;
};
mkHypervImage = modules:
nixos-generators.nixosGenerate {
inherit system;
inherit pkgs;
lib = nixpkgs.lib;
nixosSystem = nixpkgs.lib.nixosSystem;
format = "hyperv";
modules = modules;
specialArgs = {
inherit self;
};
};
in {
packages.${system} = {
spotibox-vhdx = mkHypervImage [
./machines/spotibox.nix
];
spotibox-debug-vhdx = mkHypervImage [
./machines/spotibox-debug.nix
];
qubix-manifest-json = pkgs.writeText "qubix-manifest.json"
(builtins.toJSON qubixManifest);
default = self.packages.${system}.spotibox-vhdx;
};
checks.${system}.spotibox-basic =
import ./tests/spotibox-basic.nix {
inherit pkgs;
};
};
}