From 93e10d0aea0430a174348300a9bb0ac9667f33d2 Mon Sep 17 00:00:00 2001 From: zimbatm Date: Thu, 17 Jan 2019 14:18:00 +0000 Subject: [PATCH] central command (#8) * WIP: eval target * rename nixos-build to nixos-generate * rename target to format * allow to pass arbitrary format paths * add --help option * add --list option * port the rest of the formats * add short options * be more precise with the file selection * use new trick I learned * support the --run option for the no-gui version: ./nixos-generate --run -f vm-no-gui * document * fixup! port the rest of the formats * fix kexec-bundle --- README.md | 45 ++++++++------ bin/make-gce | 5 -- bin/make-install-iso | 5 -- bin/make-iso | 5 -- bin/make-kexec | 5 -- bin/make-kexec-bundle | 4 -- bin/make-openstack | 5 -- bin/make-qcow | 5 -- bin/make-virtualbox | 5 -- bin/run-vm | 5 -- bin/run-vm-nogui | 5 -- eval-format.nix | 24 +++++++ {lib => formats}/gce.nix | 3 +- {lib => formats}/install-iso.nix | 2 +- {lib => formats}/iso.nix | 2 +- formats/kexec-bundle.nix | 6 ++ {lib => formats}/kexec.nix | 4 +- {lib => formats}/openstack.nix | 3 +- {lib => formats}/qcow.nix | 6 +- {lib => formats}/virtualbox.nix | 3 +- {lib => formats}/vm-nogui.nix | 0 {lib => formats}/vm.nix | 3 +- nixos-generate | 103 +++++++++++++++++++++++++++++++ 23 files changed, 172 insertions(+), 81 deletions(-) delete mode 100755 bin/make-gce delete mode 100755 bin/make-install-iso delete mode 100755 bin/make-iso delete mode 100755 bin/make-kexec delete mode 100755 bin/make-kexec-bundle delete mode 100755 bin/make-openstack delete mode 100755 bin/make-qcow delete mode 100755 bin/make-virtualbox delete mode 100755 bin/run-vm delete mode 100755 bin/run-vm-nogui create mode 100644 eval-format.nix rename {lib => formats}/gce.nix (75%) rename {lib => formats}/install-iso.nix (93%) rename {lib => formats}/iso.nix (88%) create mode 100644 formats/kexec-bundle.nix rename {lib => formats}/kexec.nix (98%) rename {lib => formats}/openstack.nix (76%) rename {lib => formats}/qcow.nix (92%) rename {lib => formats}/virtualbox.nix (74%) rename {lib => formats}/vm-nogui.nix (100%) rename {lib => formats}/vm.nix (92%) create mode 100755 nixos-generate diff --git a/README.md b/README.md index 9295407..e333e2a 100644 --- a/README.md +++ b/README.md @@ -1,32 +1,37 @@ -A simple collection of nixos image builders. +# nixos-generators - one config, multiple formats + +The nixos-generators project allows to take the same NixOS configuration, and +generate outputs for different target formats. + Just put your stuff into the config.nix and then call one of the image builders. for example: ``` -bin/make-iso +./nixos-generate -f iso ``` or + ``` -bin/make-iso config.nix +./nixos-generate -f iso -c /etc/nixos/configuration.nix ``` -it echoes the path to a iso image, which you then can flash onto an usb-stick or mount & boot in a virtual machine. +it echoes the path to a iso image, which you then can flash onto an usb-stick +or mount & boot in a virtual machine. -we currently have following generators: +## Supported formats -format | script | description ---- | --- | --- -gce | bin/make-gce | Google Compute Image -iso | bin/make-iso -kexec | bin/make-kexec -kexec-bundle | bin/make-kexec-bundle -openstack | bin/make-openstack -qcow2 | bin/make-qcow -virtualbox | bin/make-virtualbox - -we also have following runners: - -platform | script +format | description --- | --- -qemu-kvm | bin/run-vm -qemu-kvm (without gui) | bin/run-vm-nogui +gce | Google Compute Image +iso | Installer ISO +kexec | +kexec-bundle | +openstack | +qcow2 | +virtualbox | +vm | only used as a qemu-kvm runner +vm-nogui | same as before, but without a GUI + +## Usage + +Run `./nixos-generate --help` for detailed usage information diff --git a/bin/make-gce b/bin/make-gce deleted file mode 100755 index 70ede03..0000000 --- a/bin/make-gce +++ /dev/null @@ -1,5 +0,0 @@ -#!/bin/sh -CONFIG=${1:-config.nix} -shift -out=$(nix-build --no-out-link '' -A config.system.build.googleComputeImage -I nixos-config=lib/gce.nix -I nixcfg=${CONFIG} "$@" ) -echo "$out"/*.tar.gz diff --git a/bin/make-install-iso b/bin/make-install-iso deleted file mode 100755 index 4875ad5..0000000 --- a/bin/make-install-iso +++ /dev/null @@ -1,5 +0,0 @@ -#!/bin/sh -CONFIG=${1:-config.nix} -shift -ISO_DIR=$( nix-build --no-out-link '' -A config.system.build.isoImage -I nixos-config=lib/install-iso.nix -I nixcfg=${CONFIG} "$@" ) -find $ISO_DIR/iso/ -type f diff --git a/bin/make-iso b/bin/make-iso deleted file mode 100755 index 0e3f03c..0000000 --- a/bin/make-iso +++ /dev/null @@ -1,5 +0,0 @@ -#!/bin/sh -CONFIG=${1:-config.nix} -shift -ISO_DIR=$( nix-build --no-out-link '' -A config.system.build.isoImage -I nixos-config=lib/iso.nix -I nixcfg=${CONFIG} "$@" ) -echo $ISO_DIR/iso/nixos.iso diff --git a/bin/make-kexec b/bin/make-kexec deleted file mode 100755 index ffd5255..0000000 --- a/bin/make-kexec +++ /dev/null @@ -1,5 +0,0 @@ -#!/bin/sh -CONFIG=${1:-config.nix} -shift -TAR_DIR=$( nix-build --no-out-link '' -A config.system.build.kexec_tarball -I nixos-config=lib/kexec.nix -I nixcfg=${CONFIG} "$@" ) -find $TAR_DIR/tarball -type f diff --git a/bin/make-kexec-bundle b/bin/make-kexec-bundle deleted file mode 100755 index d9b4659..0000000 --- a/bin/make-kexec-bundle +++ /dev/null @@ -1,4 +0,0 @@ -#!/bin/sh -CONFIG=${1:-config.nix} -shift -nix-build --no-out-link '' -A config.system.build.kexec_bundle -I nixos-config=lib/kexec.nix -I nixcfg=${CONFIG} "$@" diff --git a/bin/make-openstack b/bin/make-openstack deleted file mode 100755 index 2968677..0000000 --- a/bin/make-openstack +++ /dev/null @@ -1,5 +0,0 @@ -#!/bin/sh -CONFIG=${1:-config.nix} -shift -OPENSTACK_DIR=$( nix-build --no-out-link '' -A config.system.build.novaImage -I nixos-config=lib/openstack.nix -I nixcfg=${CONFIG} "$@" ) -echo $OPENSTACK_DIR/*.qcow2 diff --git a/bin/make-qcow b/bin/make-qcow deleted file mode 100755 index ce4c8f5..0000000 --- a/bin/make-qcow +++ /dev/null @@ -1,5 +0,0 @@ -#!/bin/sh -CONFIG=${1:-config.nix} -shift -QCOW_DIR=$( nix-build --no-out-link '' -A config.system.build.qcow -I nixos-config=lib/qcow.nix -I nixcfg=${CONFIG} "$@" ) -echo $QCOW_DIR/nixos.qcow2 diff --git a/bin/make-virtualbox b/bin/make-virtualbox deleted file mode 100755 index 12b6b8c..0000000 --- a/bin/make-virtualbox +++ /dev/null @@ -1,5 +0,0 @@ -#!/bin/sh -CONFIG=${1:-config.nix} -shift -VBOX_DIR=$( nix-build --no-out-link '' -A config.system.build.virtualBoxOVA -I nixos-config=lib/virtualbox.nix -I nixcfg=${CONFIG} "$@" ) -echo $VBOX_DIR/*.ova diff --git a/bin/run-vm b/bin/run-vm deleted file mode 100755 index b189175..0000000 --- a/bin/run-vm +++ /dev/null @@ -1,5 +0,0 @@ -#!/bin/sh -CONFIG=${1:-config.nix} -shift -VM=$( nix-build --no-out-link '' -A config.system.build.vm -I nixos-config=lib/vm.nix -I nixcfg=${CONFIG} "$@" ) -${VM}/bin/run-nixos-vm diff --git a/bin/run-vm-nogui b/bin/run-vm-nogui deleted file mode 100755 index 575c6b1..0000000 --- a/bin/run-vm-nogui +++ /dev/null @@ -1,5 +0,0 @@ -#!/bin/sh -CONFIG=${1:-config.nix} -shift -VM=$( nix-build --no-out-link '' -A config.system.build.vm -I nixos-config=lib/vm-nogui.nix -I nixcfg=${CONFIG} "$@" ) -${VM}/bin/run-nixos-vm diff --git a/eval-format.nix b/eval-format.nix new file mode 100644 index 0000000..094066d --- /dev/null +++ b/eval-format.nix @@ -0,0 +1,24 @@ +{ nixpkgs ? +, configuration ? + import "${toString nixpkgs}/nixos/lib/from-env.nix" "NIXOS_CONFIG" +, formatConfig +, system ? builtins.currentSystem +}: +let + module = { lib, ... }: { + options = { + formatAttr = lib.mkOption { + type = lib.types.str; + description = "Declare the default attribute to build"; + }; + }; + }; +in +import "${toString nixpkgs}/nixos/lib/eval-config.nix" { + inherit system; + modules = [ + module + formatConfig + configuration + ]; +} diff --git a/lib/gce.nix b/formats/gce.nix similarity index 75% rename from lib/gce.nix rename to formats/gce.nix index ea3bd28..8910efa 100644 --- a/lib/gce.nix +++ b/formats/gce.nix @@ -2,6 +2,7 @@ { imports = [ "${toString modulesPath}/virtualisation/google-compute-image.nix" - ]; + + formatAttr = "googleComputeImage"; } diff --git a/lib/install-iso.nix b/formats/install-iso.nix similarity index 93% rename from lib/install-iso.nix rename to formats/install-iso.nix index 2aea3a8..a697194 100644 --- a/lib/install-iso.nix +++ b/formats/install-iso.nix @@ -2,7 +2,6 @@ { imports = [ - ]; # for installer @@ -12,4 +11,5 @@ systemd.services.wpa_supplicant.wantedBy = lib.mkForce [ "multi-user.target" ]; systemd.services.sshd.wantedBy = lib.mkForce [ "multi-user.target" ]; + formatAttr = "isoImage"; } diff --git a/lib/iso.nix b/formats/iso.nix similarity index 88% rename from lib/iso.nix rename to formats/iso.nix index ec039b7..9b0981f 100644 --- a/lib/iso.nix +++ b/formats/iso.nix @@ -2,7 +2,6 @@ { imports = [ - ]; # EFI booting @@ -11,4 +10,5 @@ # USB booting isoImage.makeUsbBootable = true; + formatAttr = "isoImage"; } diff --git a/formats/kexec-bundle.nix b/formats/kexec-bundle.nix new file mode 100644 index 0000000..e18bce0 --- /dev/null +++ b/formats/kexec-bundle.nix @@ -0,0 +1,6 @@ +{ lib, ... }: +{ + imports = [ ./kexec.nix ]; + + formatAttr = lib.mkForce "kexec_bundle"; +} diff --git a/lib/kexec.nix b/formats/kexec.nix similarity index 98% rename from lib/kexec.nix rename to formats/kexec.nix index bbc1141..9bbbf09 100644 --- a/lib/kexec.nix +++ b/formats/kexec.nix @@ -7,7 +7,6 @@ in { imports = [ - "${clever-tests}/kexec/autoreboot.nix" "${clever-tests}/kexec/kexec.nix" "${clever-tests}/kexec/justdoit.nix" @@ -54,5 +53,6 @@ in { ]; systemd.services.sshd.wantedBy = lib.mkForce [ "multi-user.target" ]; networking.hostName = lib.mkDefault "kexec"; -} + formatAttr = "kexec_tarball"; +} diff --git a/lib/openstack.nix b/formats/openstack.nix similarity index 76% rename from lib/openstack.nix rename to formats/openstack.nix index 23d5c76..96452d3 100644 --- a/lib/openstack.nix +++ b/formats/openstack.nix @@ -2,6 +2,7 @@ { imports = [ - ]; + + formatAttr = "novaImage"; } diff --git a/lib/qcow.nix b/formats/qcow.nix similarity index 92% rename from lib/qcow.nix rename to formats/qcow.nix index 48a3535..b56f326 100644 --- a/lib/qcow.nix +++ b/formats/qcow.nix @@ -1,9 +1,5 @@ { config, lib, pkgs, ... }: { - imports = [ - - ]; - fileSystems."/" = { device = "/dev/disk/by-label/nixos"; autoResize = true; @@ -20,4 +16,6 @@ diskSize = 8192; format = "qcow2"; }; + + formatAttr = "qcow"; } diff --git a/lib/virtualbox.nix b/formats/virtualbox.nix similarity index 74% rename from lib/virtualbox.nix rename to formats/virtualbox.nix index a726a09..eac23be 100644 --- a/lib/virtualbox.nix +++ b/formats/virtualbox.nix @@ -2,6 +2,7 @@ { imports = [ - ]; + + formatAttr = "virtualBoxOVA"; } diff --git a/lib/vm-nogui.nix b/formats/vm-nogui.nix similarity index 100% rename from lib/vm-nogui.nix rename to formats/vm-nogui.nix diff --git a/lib/vm.nix b/formats/vm.nix similarity index 92% rename from lib/vm.nix rename to formats/vm.nix index 450aa68..09a3adc 100644 --- a/lib/vm.nix +++ b/formats/vm.nix @@ -2,11 +2,12 @@ { imports = [ - ]; virtualisation.qemu.networkingOptions = [ "-net nic,netdev=user.0,model=virtio" "-netdev user,id=user.0\${QEMU_NET_OPTS:+,$QEMU_NET_OPTS},hostfwd=tcp::8088-:80,hostfwd=tcp::8022-:22" ]; + + formatAttr = "vm"; } diff --git a/nixos-generate b/nixos-generate new file mode 100755 index 0000000..71d5a3f --- /dev/null +++ b/nixos-generate @@ -0,0 +1,103 @@ +#!/usr/bin/env bash +set -euo pipefail + +## Configuration + +libexec_dir="${0%/*}" +configuration_path=${NIX_CONFIG:-$libexec_dir/config.nix} +format_dir=$libexec_dir/formats +format_path= +run= + +## Functions + +showUsage() { + cat <&2 + exit 1 +} + +## Main ## + +while [[ $# -gt 0 ]]; do + case "$1" in + -c | --configuration) + configuration_path=$2 + shift + ;; + -f | --format) + format_path=$format_dir/$2.nix + shift + ;; + --format-path) + format_path=$2 + shift + ;; + --help) + showUsage + exit + ;; + --list) + listFormats + exit + ;; + --run) + run=1 + # default to the VM format + if [[ -z $format_path ]]; then + format_path=$format_dir/vm.nix + fi + ;; + *) + abort "unknown option $1" + ;; + esac + shift +done + +if [[ -z $format_path ]]; then + abort "missing format. use --help for more details" +fi + +if [[ ! -e $format_path ]]; then + abort "format '$format_path' not found" +fi + +args=( + "$libexec_dir/eval-format.nix" + --arg configuration "$configuration_path" + --arg formatConfig "$format_path" +) + +formatAttr=$(nix-instantiate "${args[@]}" --eval --json -A config.formatAttr | jq -r .) + +out=$(nix-build "${args[@]}" --no-out-link -A "config.system.build.$formatAttr") + +if [[ -z $run ]]; then + # show the first file + find "$out" -type f -print -quit +else + "$out/bin/run-nixos-vm" +fi