From e275808313f164f797cb99660621cf5b62ecf2a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicolas=20=E2=80=9CNiols=E2=80=9D=20Jeannerod?= Date: Mon, 11 Nov 2024 15:44:04 +0100 Subject: [PATCH] Allow all ids between 100 and 255 --- flake.nix | 35 ++++++++++++++++++++--------------- procolixVm.nix | 23 +++++++++++++++++------ provision-vm.sh | 2 +- 3 files changed, 38 insertions(+), 22 deletions(-) diff --git a/flake.nix b/flake.nix index c3449893..85be7cfe 100644 --- a/flake.nix +++ b/flake.nix @@ -44,18 +44,23 @@ devShells.default = pkgs.mkShell { packages = [ inputs'.nixops4.packages.default ]; }; }; - flake.allVmIds = [ - "101" - "102" - "103" - "104" - "105" - "106" - "107" - "108" - "109" - "110" - ]; + flake.vmIdTo03d = + id: + let + sid = toString id; + in + if id >= 0 && id <= 9 then + "00${sid}" + else if id >= 10 && id <= 99 then + "0${sid}" + else + sid; + + flake.allVmIds = # 100 -- 255 + let + allVmIdsFrom = x: if x > 255 then [ ] else [ x ] ++ allVmIdsFrom (x + 1); + in + allVmIdsFrom 100; flake.nixosConfigurations.provisioning = let @@ -72,7 +77,7 @@ in listToAttrs ( map (vmid: { - name = "fedi${vmid}"; + name = "fedi${self.vmIdTo03d vmid}"; value = makeProvisioningConfiguration vmid; }) self.allVmIds ); @@ -108,8 +113,8 @@ type = providers.local.exec; imports = [ inputs.nixops4-nixos.modules.nixops4Resource.nixos ]; ssh.opts = ""; - ssh.host = "95.215.187.${vmid}"; - ssh.hostPublicKey = readFile ./hostKeys/fedi${vmid}/ssh_host_ed25519_key.pub; + ssh.host = "95.215.187.${self.vmIdTo03d vmid}"; + ssh.hostPublicKey = readFile ./hostKeys/fedi${self.vmIdTo03d vmid}/ssh_host_ed25519_key.pub; nixpkgs = inputs.nixpkgs; nixos.module = diff --git a/procolixVm.nix b/procolixVm.nix index 38ebc7a9..76efeb1d 100644 --- a/procolixVm.nix +++ b/procolixVm.nix @@ -9,17 +9,28 @@ let inherit (lib) mkOption; inherit (lib.types) types; + vmIdTo03d = + id: + let + sid = toString id; + in + if id >= 0 && id <= 9 then + "00${sid}" + else if id >= 10 && id <= 99 then + "0${sid}" + else + sid; in + { imports = [ (modulesPath + "/profiles/qemu-guest.nix") ]; options = { procolix = { vmid = mkOption { - type = types.str; + type = types.int; description = '' - Identifier of the machine. This is a number between 10 and 255, padded - with zeroes to always be three characters. + Identifier of the machine. This is a number between 10 and 255. ''; }; }; @@ -32,7 +43,7 @@ in services.openssh.enable = true; networking = { - hostName = "fedi${config.procolix.vmid}"; + hostName = "fedi${vmIdTo03d config.procolix.vmid}"; domain = "procolix.com"; interfaces = { @@ -40,7 +51,7 @@ in ipv4 = { addresses = [ { - address = "95.215.187.${config.procolix.vmid}"; + address = "95.215.187.${vmIdTo03d config.procolix.vmid}"; prefixLength = 24; } ]; @@ -48,7 +59,7 @@ in ipv6 = { addresses = [ { - address = "2a00:51c0:13:1305::${config.procolix.vmid}"; + address = "2a00:51c0:13:1305::${vmIdTo03d config.procolix.vmid}"; prefixLength = 64; } ]; diff --git a/provision-vm.sh b/provision-vm.sh index 38420f42..c624fd79 100755 --- a/provision-vm.sh +++ b/provision-vm.sh @@ -162,7 +162,7 @@ printf 'Creating VM...' http_ --form POST $apiurl/nodes/$node/qemu \ \ vmid==$vmid \ - name==fedi$vmid \ + name==$(printf 'fedi%03d' $vmid) \ pool==Fediversity \ \ ide2=="local:iso/$isoname,media=cdrom" \