From 82513c07142010ca05ac16e131c2866796d89b72 Mon Sep 17 00:00:00 2001 From: Sagar Ch Date: Thu, 23 May 2024 17:01:10 +0000 Subject: [PATCH] refactor names for customs and templates for more clarity. templates are self contained instead of depending on others --- flake.nix | 2 +- lib/images/debian/customs.nix | 82 +++++++++--------------------- lib/images/debian/default.nix | 16 +++--- lib/images/debian/templates.nix | 89 +++++++++++++++++++++++++-------- 4 files changed, 101 insertions(+), 88 deletions(-) diff --git a/flake.nix b/flake.nix index 60f3421..8ad9dec 100644 --- a/flake.nix +++ b/flake.nix @@ -17,7 +17,7 @@ name = "playfulness"; }; - nixmox = customizeImage images.debian.v12.proxmox (images.debian.customs.rooted // { + nixmox = customizeImage images.debian.v12.proxmox (images.debian.templates.rooted // { name = "nixmox"; }); diff --git a/lib/images/debian/customs.nix b/lib/images/debian/customs.nix index 5796692..1b6932c 100644 --- a/lib/images/debian/customs.nix +++ b/lib/images/debian/customs.nix @@ -1,63 +1,27 @@ -# ready to use customizations to apply on images -{ pkgs, lib, system, commons, ... }: +# create additional useful customized images from templates and upstream images +{ pkgs, lib, system, commons, upstreamImages, templates, ... }: with commons; with scriptsNFiles; +let + upstreamImageName = "v12"; +in { - # essential functionalities like ssh, networking etc - essentials = { - install = [ "htop" "openssh-server" "inetutils-ping" "dnsutils" "cloud-guest-utils" "qemu-guest-agent" ]; - commands = '' - upload ${grub-ifnames-0}:/etc/default/grub.d/90-ifnames-0.cfg - upload ${grub-disable-microcode}:/etc/default/grub.d/00-disable-microcode.cfg - run-command mount /boot/efi && update-grub - upload ${eth0-dhcp-network}:/etc/systemd/network/00-eth0-dhcp.network - run ${ssh-service-override-conf-create} - upload ${grow-root-sh}:/usr/local/sbin/grow-root.sh - upload ${grow-root-service}:/etc/systemd/system/grow-root.service - run-command systemctl enable grow-root.service - ''; + ${upstreamImageName} = rec { + # default image with essential functionalities like ssh, networking etc + default = customizeImage upstreamImages.${upstreamImageName} (templates.essentials // { + name = "default"; + hostname = "debian"; + }); + + # playground with easy root access + play = customizeImage default (templates.rooted // { + name = "play"; + nameToHostname = false; + }); + + # proxmox + proxmox = customizeImage default (templates.proxmoxOnDebian12 // { + name = "proxmox"; + }); }; - - # set easy root access - rooted = { - run = '' - # set root password and ssh access - echo "root:root" | chpasswd - sed -i '/PasswordAuthentication no/d' "/etc/ssh/sshd_config" - echo "PasswordAuthentication yes\nPermitRootLogin yes" >> "/etc/ssh/sshd_config" - ''; - }; - - # install proxmox - proxmoxOnDebian12 = { - diskSize = "+2G"; - smp = 4; - memSize = 4096; - run = '' - # script originally taken from https://pve.proxmox.com/wiki/Install_Proxmox_VE_on_Debian_12_Bookworm - # exit if error - set -e - - # grow root partition - script installed in "base" image - /usr/local/sbin/grow-root.sh - - # mount efi for grub changes - mount /boot/efi - - # add proxmox repo - echo "deb [arch=amd64] http://download.proxmox.com/debian/pve bookworm pve-no-subscription" > /etc/apt/sources.list.d/pve-install-repo.list - wget https://enterprise.proxmox.com/debian/proxmox-release-bookworm.gpg -O /etc/apt/trusted.gpg.d/proxmox-release-bookworm.gpg - apt-get update && apt full-upgrade -y --no-install-recommends; - - # necessary precursors - echo "0.0.0.0\t\t`cat /etc/hostname`" >> /etc/hosts; # necessary for SSL certificate creation - mkdir -p /run/network; # bug https://github.com/CumulusNetworks/ifupdown2/issues/276 - - # install - apt install -y proxmox-default-kernel proxmox-ve postfix open-iscsi chrony --no-install-recommends; - - # remove previous kernels - apt remove -y os-prober linux-image-amd64 'linux-image-6.*'; - ''; - }; -} +} \ No newline at end of file diff --git a/lib/images/debian/default.nix b/lib/images/debian/default.nix index fa511b1..2a7eeb8 100644 --- a/lib/images/debian/default.nix +++ b/lib/images/debian/default.nix @@ -3,14 +3,14 @@ let # upstream distro images upstreamImagesJSON = lib.importJSON ./upstream.json; upstreamImages = lib.mapAttrs (name: src: pkgs.fetchurl src) upstreamImagesJSON.${system}; - customs = (import ./customs.nix) { inherit pkgs lib system commons; }; - templates = (import ./templates.nix) { inherit pkgs lib system commons upstreamImages customs; }; - mergeUpstreamImageAndTemplates = - name: image: + templates = (import ./templates.nix) { inherit pkgs lib system commons; }; + customs = (import ./customs.nix) { inherit pkgs lib system commons upstreamImages templates; }; + mergeUpstreamAndCustomImages = + name: upstreamImage: let - imageTemplates = lib.optionalAttrs (lib.hasAttr "${name}" templates) templates.${name}; + customImages = lib.optionalAttrs (lib.hasAttr "${name}" customs) customs.${name}; in - imageTemplates // { upstream = image; }; + customImages // { upstream = upstreamImage; }; - images = lib.mapAttrs mergeUpstreamImageAndTemplates upstreamImages; -in images // { inherit customs; } \ No newline at end of file + images = lib.mapAttrs mergeUpstreamAndCustomImages upstreamImages; +in images // { inherit templates; } \ No newline at end of file diff --git a/lib/images/debian/templates.nix b/lib/images/debian/templates.nix index a9db3da..37d5fcb 100644 --- a/lib/images/debian/templates.nix +++ b/lib/images/debian/templates.nix @@ -1,24 +1,73 @@ -# create additional useful template images from upstream images -{ pkgs, lib, system, commons, upstreamImages, customs, ... }: +# ready to use customization templates to apply on images +{ pkgs, lib, system, commons, ... }: with commons; with scriptsNFiles; { - v12 = rec { - # default image with essential functionalities like ssh, networking etc - default = customizeImage upstreamImages.v12 (customs.essentials // { - name = "default"; - hostname = "debian"; - }); - - # playground with easy root access - play = customizeImage default (customs.rooted // { - name = "play"; - nameToHostname = false; - }); - - # proxmox - proxmox = customizeImage default (customs.proxmoxOnDebian12 // { - name = "proxmox"; - }); + # essential functionalities like ssh, networking etc + essentials = { + install = [ "htop" "openssh-server" "inetutils-ping" "dnsutils" "cloud-guest-utils" "qemu-guest-agent" ]; + commands = '' + upload ${grub-ifnames-0}:/etc/default/grub.d/90-ifnames-0.cfg + upload ${grub-disable-microcode}:/etc/default/grub.d/00-disable-microcode.cfg + run-command mount /boot/efi && update-grub + upload ${eth0-dhcp-network}:/etc/systemd/network/00-eth0-dhcp.network + run ${ssh-service-override-conf-create} + upload ${grow-root-sh}:/usr/local/sbin/grow-root.sh + upload ${grow-root-service}:/etc/systemd/system/grow-root.service + run-command systemctl enable grow-root.service + ''; }; -} \ No newline at end of file + + # set easy root access + rooted = { + install = [ "openssh-server" ]; + commands = '' + run ${ssh-service-override-conf-create} + ''; + run = '' + # set root password and ssh access + echo "root:root" | chpasswd + sed -i '/PasswordAuthentication no/d' "/etc/ssh/sshd_config" + echo "PasswordAuthentication yes\nPermitRootLogin yes" >> "/etc/ssh/sshd_config" + ''; + }; + + # install proxmox + proxmoxOnDebian12 = { + diskSize = "+2G"; + smp = 4; + memSize = 4096; + install = [ "cloud-guest-utils" ]; + commands = '' + upload ${grow-root-sh}:/usr/local/sbin/grow-root.sh + upload ${grow-root-service}:/etc/systemd/system/grow-root.service + run-command systemctl enable grow-root.service + ''; + run = '' + # script originally taken from https://pve.proxmox.com/wiki/Install_Proxmox_VE_on_Debian_12_Bookworm + # exit if error + set -e + + # grow root partition + /usr/local/sbin/grow-root.sh + + # mount efi for grub changes + mount /boot/efi + + # add proxmox repo + echo "deb [arch=amd64] http://download.proxmox.com/debian/pve bookworm pve-no-subscription" > /etc/apt/sources.list.d/pve-install-repo.list + wget https://enterprise.proxmox.com/debian/proxmox-release-bookworm.gpg -O /etc/apt/trusted.gpg.d/proxmox-release-bookworm.gpg + apt-get update && apt full-upgrade -y --no-install-recommends; + + # necessary precursors + echo "0.0.0.0\t\t`cat /etc/hostname`" >> /etc/hosts; # necessary for SSL certificate creation + mkdir -p /run/network; # bug https://github.com/CumulusNetworks/ifupdown2/issues/276 + + # install + apt install -y proxmox-default-kernel proxmox-ve postfix open-iscsi chrony --no-install-recommends; + + # remove previous kernels + apt remove -y os-prober linux-image-amd64 'linux-image-6.*'; + ''; + }; +}