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
This commit is contained in:
zimbatm 2019-01-17 14:18:00 +00:00 committed by Lassulus
parent 400b73364a
commit 93e10d0aea
23 changed files with 172 additions and 81 deletions

View File

@ -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

View File

@ -1,5 +0,0 @@
#!/bin/sh
CONFIG=${1:-config.nix}
shift
out=$(nix-build --no-out-link '<nixpkgs/nixos>' -A config.system.build.googleComputeImage -I nixos-config=lib/gce.nix -I nixcfg=${CONFIG} "$@" )
echo "$out"/*.tar.gz

View File

@ -1,5 +0,0 @@
#!/bin/sh
CONFIG=${1:-config.nix}
shift
ISO_DIR=$( nix-build --no-out-link '<nixpkgs/nixos>' -A config.system.build.isoImage -I nixos-config=lib/install-iso.nix -I nixcfg=${CONFIG} "$@" )
find $ISO_DIR/iso/ -type f

View File

@ -1,5 +0,0 @@
#!/bin/sh
CONFIG=${1:-config.nix}
shift
ISO_DIR=$( nix-build --no-out-link '<nixpkgs/nixos>' -A config.system.build.isoImage -I nixos-config=lib/iso.nix -I nixcfg=${CONFIG} "$@" )
echo $ISO_DIR/iso/nixos.iso

View File

@ -1,5 +0,0 @@
#!/bin/sh
CONFIG=${1:-config.nix}
shift
TAR_DIR=$( nix-build --no-out-link '<nixpkgs/nixos>' -A config.system.build.kexec_tarball -I nixos-config=lib/kexec.nix -I nixcfg=${CONFIG} "$@" )
find $TAR_DIR/tarball -type f

View File

@ -1,4 +0,0 @@
#!/bin/sh
CONFIG=${1:-config.nix}
shift
nix-build --no-out-link '<nixpkgs/nixos>' -A config.system.build.kexec_bundle -I nixos-config=lib/kexec.nix -I nixcfg=${CONFIG} "$@"

View File

@ -1,5 +0,0 @@
#!/bin/sh
CONFIG=${1:-config.nix}
shift
OPENSTACK_DIR=$( nix-build --no-out-link '<nixpkgs/nixos>' -A config.system.build.novaImage -I nixos-config=lib/openstack.nix -I nixcfg=${CONFIG} "$@" )
echo $OPENSTACK_DIR/*.qcow2

View File

@ -1,5 +0,0 @@
#!/bin/sh
CONFIG=${1:-config.nix}
shift
QCOW_DIR=$( nix-build --no-out-link '<nixpkgs/nixos>' -A config.system.build.qcow -I nixos-config=lib/qcow.nix -I nixcfg=${CONFIG} "$@" )
echo $QCOW_DIR/nixos.qcow2

View File

@ -1,5 +0,0 @@
#!/bin/sh
CONFIG=${1:-config.nix}
shift
VBOX_DIR=$( nix-build --no-out-link '<nixpkgs/nixos>' -A config.system.build.virtualBoxOVA -I nixos-config=lib/virtualbox.nix -I nixcfg=${CONFIG} "$@" )
echo $VBOX_DIR/*.ova

View File

@ -1,5 +0,0 @@
#!/bin/sh
CONFIG=${1:-config.nix}
shift
VM=$( nix-build --no-out-link '<nixpkgs/nixos>' -A config.system.build.vm -I nixos-config=lib/vm.nix -I nixcfg=${CONFIG} "$@" )
${VM}/bin/run-nixos-vm

View File

@ -1,5 +0,0 @@
#!/bin/sh
CONFIG=${1:-config.nix}
shift
VM=$( nix-build --no-out-link '<nixpkgs/nixos>' -A config.system.build.vm -I nixos-config=lib/vm-nogui.nix -I nixcfg=${CONFIG} "$@" )
${VM}/bin/run-nixos-vm

24
eval-format.nix Normal file
View File

@ -0,0 +1,24 @@
{ nixpkgs ? <nixpkgs>
, configuration ?
import "${toString nixpkgs}/nixos/lib/from-env.nix" "NIXOS_CONFIG" <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
];
}

View File

@ -2,6 +2,7 @@
{
imports = [
"${toString modulesPath}/virtualisation/google-compute-image.nix"
<nixcfg>
];
formatAttr = "googleComputeImage";
}

View File

@ -2,7 +2,6 @@
{
imports = [
<nixpkgs/nixos/modules/installer/cd-dvd/installation-cd-base.nix>
<nixcfg>
];
# 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";
}

View File

@ -2,7 +2,6 @@
{
imports = [
<nixpkgs/nixos/modules/installer/cd-dvd/iso-image.nix>
<nixcfg>
];
# EFI booting
@ -11,4 +10,5 @@
# USB booting
isoImage.makeUsbBootable = true;
formatAttr = "isoImage";
}

6
formats/kexec-bundle.nix Normal file
View File

@ -0,0 +1,6 @@
{ lib, ... }:
{
imports = [ ./kexec.nix ];
formatAttr = lib.mkForce "kexec_bundle";
}

View File

@ -7,7 +7,6 @@
in {
imports = [
<nixpkgs/nixos/modules/installer/netboot/netboot-minimal.nix>
<nixcfg>
"${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";
}

View File

@ -2,6 +2,7 @@
{
imports = [
<nixpkgs/nixos/maintainers/scripts/openstack/nova-image.nix>
<nixcfg>
];
formatAttr = "novaImage";
}

View File

@ -1,9 +1,5 @@
{ config, lib, pkgs, ... }:
{
imports = [
<nixcfg>
];
fileSystems."/" = {
device = "/dev/disk/by-label/nixos";
autoResize = true;
@ -20,4 +16,6 @@
diskSize = 8192;
format = "qcow2";
};
formatAttr = "qcow";
}

View File

@ -2,6 +2,7 @@
{
imports = [
<nixpkgs/nixos/modules/virtualisation/virtualbox-image.nix>
<nixcfg>
];
formatAttr = "virtualBoxOVA";
}

View File

@ -2,11 +2,12 @@
{
imports = [
<nixpkgs/nixos/modules/virtualisation/qemu-vm.nix>
<nixcfg>
];
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";
}

103
nixos-generate Executable file
View File

@ -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 <<USAGE
Usage: $0 [options]
Options:
* --help: shows this help
* -c, --configuration PATH:
select the nixos configuration to build. Default: $configuration_path
* -f, --format NAME: select one of the pre-determined formats
* --format-path PATH: pass a custom format
* --list: list the available built-in formats
* --run: runs the configuration in a VM
only works for the "vm" and "vm-no-gui" formats
USAGE
}
listFormats() {
for format in "$format_dir"/*.nix; do
basename "$format" ".nix"
done
}
abort() {
echo "aborted: $*" >&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