Merge pull request #240 from DavHau/improvements

This commit is contained in:
Lassulus 2023-05-30 10:48:47 +02:00 committed by GitHub
commit 7abb7b6405
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
38 changed files with 300 additions and 199 deletions

View File

@ -16,5 +16,5 @@ jobs:
- uses: cachix/install-nix-action@v21
- name: List flake structure
run: nix flake show
- name: Run unit tests (flake)
run: nix build -L
- name: Run tests
run: nix flake check -L

5
checks/is-formatted.nix Normal file
View File

@ -0,0 +1,5 @@
{pkgs ? import <nixpkgs> {}}:
pkgs.runCommand "check-format" {} ''
${pkgs.alejandra}/bin/alejandra -c ${./.}
touch $out
''

View File

@ -1,10 +1,14 @@
{ config, lib, pkgs, ... }:
{
config,
lib,
pkgs,
...
}: {
services.sshd.enable = true;
services.nginx.enable = true;
networking.firewall.allowedTCPPorts = [ 80 ];
networking.firewall.allowedTCPPorts = [80];
users.users.root.password = "nixos";
services.openssh.permitRootLogin = lib.mkDefault "yes";
services.getty.autologinUser = lib.mkDefault "root";

View File

@ -1,3 +1,5 @@
(import (builtins.fetchTarball https://github.com/edolstra/flake-compat/archive/master.tar.gz) {
src = ./.;
}).defaultNix.default
})
.defaultNix
.default

235
flake.nix
View File

@ -7,108 +7,145 @@
# Bin dependency
inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
outputs = { self, nixpkgs, nixlib }:
outputs = {
self,
nixpkgs,
nixlib,
}:
# Library modules (depend on nixlib)
{
# export all generator formats in ./formats
nixosModules = nixlib.lib.mapAttrs' (file: _: {
name = nixlib.lib.removeSuffix ".nix" file;
# The exported module should include the internal format* options
value.imports = [ (./formats + "/${file}") ./format-module.nix ];
}) (builtins.readDir ./formats);
{
# export all generator formats in ./formats
nixosModules = nixlib.lib.mapAttrs' (file: _: {
name = nixlib.lib.removeSuffix ".nix" file;
# The exported module should include the internal format* options
value.imports = [(./formats + "/${file}") ./format-module.nix];
}) (builtins.readDir ./formats);
# example usage in flakes:
# outputs = { self, nixpkgs, nixos-generators, ...}: {
# vmware = nixos-generators.nixosGenerate {
# system = "x86_64-linux";
# modules = [./configuration.nix];
# format = "vmware";
# };
# }
# example usage in flakes:
# outputs = { self, nixpkgs, nixos-generators, ...}: {
# vmware = nixos-generators.nixosGenerate {
# system = "x86_64-linux";
# modules = [./configuration.nix];
# format = "vmware";
# };
# }
nixosGenerate = { pkgs ? null, lib ? nixpkgs.lib, format, system ? null, specialArgs ? { }, modules ? [ ], customFormats ? {} }:
let
extraFormats = lib.mapAttrs' (name: value: lib.nameValuePair
(name)
(value // {
imports = ( value.imports or [] ++ [ ./format-module.nix ] );
} )
) customFormats;
formatModule = builtins.getAttr format (self.nixosModules // extraFormats);
image = nixpkgs.lib.nixosSystem {
inherit pkgs specialArgs;
system = if system != null then system else pkgs.system;
lib = if lib != null then lib else pkgs.lib;
modules = [
formatModule
] ++ modules;
};
in
image.config.system.build.${image.config.formatAttr};
}
//
# Binary and Devshell outputs (depend on nixpkgs)
(
let
forAllSystems = nixpkgs.lib.genAttrs [ "x86_64-linux" "x86_64-darwin" "i686-linux" "aarch64-linux" "aarch64-darwin" ];
in {
packages = forAllSystems (system: let
pkgs = nixpkgs.legacyPackages."${system}";
in rec {
nixos-generators = nixpkgs.lib.warn ''
Deprecation note from: github:nix-community/nixos-generators
Was renamed:
Was: nixos-generators.packages.${system}.nixos-generators
Now: nixos-generators.packages.${system}.nixos-generate
Plase adapt your references
'' nixos-generate;
nixos-generate = pkgs.stdenv.mkDerivation {
name = "nixos-generate";
src = ./.;
meta.description = "Collection of image builders";
nativeBuildInputs = with pkgs; [ makeWrapper ];
installFlags = [ "PREFIX=$(out)" ];
postFixup = ''
wrapProgram $out/bin/nixos-generate \
--prefix PATH : ${pkgs.lib.makeBinPath (with pkgs; [ jq coreutils findutils ])}
'';
nixosGenerate = {
pkgs ? null,
lib ? nixpkgs.lib,
format,
system ? null,
specialArgs ? {},
modules ? [],
customFormats ? {},
}: let
extraFormats =
lib.mapAttrs' (
name: value:
lib.nameValuePair
name
(value
// {
imports = value.imports or [] ++ [./format-module.nix];
})
)
customFormats;
formatModule = builtins.getAttr format (self.nixosModules // extraFormats);
image = nixpkgs.lib.nixosSystem {
inherit pkgs specialArgs;
system =
if system != null
then system
else pkgs.system;
lib =
if lib != null
then lib
else pkgs.lib;
modules =
[
formatModule
]
++ modules;
};
});
defaultPackage = forAllSystems (system: self.packages."${system}".nixos-generate);
devShell = forAllSystems (system: let
pkgs = nixpkgs.legacyPackages."${system}";
in pkgs.mkShell {
buildInputs = with pkgs; [ jq coreutils findutils ];
});
# Make it runnable with `nix run`
apps = forAllSystems (system: let
nixos-generate = {
type = "app";
program = "${self.packages."${system}".nixos-generate}/bin/nixos-generate";
};
in {
inherit nixos-generate;
# Nix >= 2.7 flake output schema uses `apps.<system>.default` instead
# of `defaultApp.<system>` to signify the default app (the thing that
# gets run with `nix run . -- <args>`)
default = nixos-generate;
});
defaultApp = forAllSystems (system: self.apps."${system}".nixos-generate);
in
image.config.system.build.${image.config.formatAttr};
}
);
//
# Binary and Devshell outputs (depend on nixpkgs)
(
let
forAllSystems = nixpkgs.lib.genAttrs ["x86_64-linux" "x86_64-darwin" "i686-linux" "aarch64-linux" "aarch64-darwin"];
in {
formatter = forAllSystems (system: nixpkgs.legacyPackages.${system}.alejandra);
packages = forAllSystems (system: let
pkgs = nixpkgs.legacyPackages."${system}";
in rec {
default = nixos-generate;
nixos-generators =
nixpkgs.lib.warn ''
Deprecation note from: github:nix-community/nixos-generators
Was renamed:
Was: nixos-generators.packages.${system}.nixos-generators
Now: nixos-generators.packages.${system}.nixos-generate
Plase adapt your references
''
nixos-generate;
nixos-generate = pkgs.stdenv.mkDerivation {
name = "nixos-generate";
src = ./.;
meta.description = "Collection of image builders";
nativeBuildInputs = with pkgs; [makeWrapper];
installFlags = ["PREFIX=$(out)"];
postFixup = ''
wrapProgram $out/bin/nixos-generate \
--prefix PATH : ${pkgs.lib.makeBinPath (with pkgs; [jq coreutils findutils])}
'';
};
});
checks = forAllSystems (system: {
inherit
(self.packages.${system})
nixos-generate
;
is-formatted = import ./checks/is-formatted.nix {
pkgs = nixpkgs.legacyPackages.${system};
};
});
devShells = forAllSystems (system: let
pkgs = nixpkgs.legacyPackages."${system}";
in {
default = pkgs.mkShell {
buildInputs = with pkgs; [jq coreutils findutils];
};
});
# Make it runnable with `nix run`
apps = forAllSystems (system: let
nixos-generate = {
type = "app";
program = "${self.packages."${system}".nixos-generate}/bin/nixos-generate";
};
in {
inherit nixos-generate;
# Nix >= 2.7 flake output schema uses `apps.<system>.default` instead
# of `defaultApp.<system>` to signify the default app (the thing that
# gets run with `nix run . -- <args>`)
default = nixos-generate;
});
# legacy flake schema compat
defaultApp = forAllSystems (system: self.apps."${system}".nixos-generate);
defaultPackage =
forAllSystems (system: self.packages.${system}.default);
devShell = forAllSystems (system: self.devShells.${system}.default);
}
);
}

View File

@ -1,4 +1,4 @@
{ lib, ... }: rec {
{lib, ...}: rec {
_file = ./format-module.nix;
# This deliberate key makes sure this module will be deduplicated
# regardless of the accessor path: either via flake's nixosModule
@ -18,4 +18,3 @@
};
};
}

View File

@ -1,5 +1,4 @@
{ modulesPath, ... }:
{
{modulesPath, ...}: {
imports = [
"${toString modulesPath}/../maintainers/scripts/ec2/amazon-image.nix"
];

View File

@ -1,5 +1,4 @@
{ modulesPath, ... }:
{
{modulesPath, ...}: {
imports = [
"${toString modulesPath}/virtualisation/azure-image.nix"
];

View File

@ -1,14 +1,20 @@
{ config, lib, pkgs, modulesPath, ... }:
{
config,
lib,
pkgs,
modulesPath,
...
}: {
imports = [
"${toString modulesPath}/virtualisation/cloudstack-config.nix"
];
system.build.cloudstackImage = import "${toString modulesPath}/../lib/make-disk-image.nix" {
system.build.cloudstackImage = import "${toString modulesPath}/../lib/make-disk-image.nix" {
inherit lib config pkgs;
diskSize = 8192;
format = "qcow2";
configFile = pkgs.writeText "configuration.nix"
configFile =
pkgs.writeText "configuration.nix"
''
{
imports = [ "${toString modulesPath}/virtualisation/cloudstack-config.nix" ];

View File

@ -1,5 +1,4 @@
{ modulesPath, ... }:
{
{modulesPath, ...}: {
imports = [
"${toString modulesPath}/virtualisation/digital-ocean-image.nix"
];

View File

@ -1,5 +1,8 @@
{ modulesPath, lib, ... }:
{
modulesPath,
lib,
...
}: {
imports = [
"${toString modulesPath}/virtualisation/docker-image.nix"
];

View File

@ -1,5 +1,4 @@
{ modulesPath, ... }:
{
{modulesPath, ...}: {
imports = [
"${toString modulesPath}/virtualisation/google-compute-image.nix"
];

View File

@ -1,5 +1,4 @@
{ modulesPath, ... }:
{
{modulesPath, ...}: {
imports = [
"${toString modulesPath}/virtualisation/hyperv-image.nix"
];

View File

@ -1,11 +1,15 @@
{ config, lib, modulesPath, ... }:
{
config,
lib,
modulesPath,
...
}: {
imports = [
"${toString modulesPath}/installer/cd-dvd/installation-cd-base.nix"
];
# override installation-cd-base and enable wpa and sshd start at boot
systemd.services.sshd.wantedBy = lib.mkForce [ "multi-user.target" ];
systemd.services.sshd.wantedBy = lib.mkForce ["multi-user.target"];
virtualisation.hypervGuest.enable = true;
formatAttr = "isoImage";

View File

@ -1,12 +1,16 @@
{ config, lib, modulesPath, ... }:
{
config,
lib,
modulesPath,
...
}: {
imports = [
"${toString modulesPath}/installer/cd-dvd/installation-cd-base.nix"
];
# override installation-cd-base and enable wpa and sshd start at boot
systemd.services.wpa_supplicant.wantedBy = lib.mkForce [ "multi-user.target" ];
systemd.services.sshd.wantedBy = lib.mkForce [ "multi-user.target" ];
systemd.services.wpa_supplicant.wantedBy = lib.mkForce ["multi-user.target"];
systemd.services.sshd.wantedBy = lib.mkForce ["multi-user.target"];
formatAttr = "isoImage";
filename = "*.iso";

View File

@ -1,5 +1,8 @@
{ config, modulesPath, ... }:
{
config,
modulesPath,
...
}: {
imports = [
"${toString modulesPath}/installer/cd-dvd/iso-image.nix"
];

View File

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

View File

@ -1,11 +1,17 @@
{ config, pkgs, lib, modulesPath, options, ... }: let
{
config,
pkgs,
lib,
modulesPath,
options,
...
}: let
clever-tests = builtins.fetchGit {
url = "https://github.com/cleverca22/nix-tests";
rev = "a9a316ad89bfd791df4953c1a8b4e8ed77995a18"; # master on 2021-06-13
};
inherit (import ../lib.nix { inherit lib options; }) maybe;
inherit (import ../lib.nix {inherit lib options;}) maybe;
in {
imports = [
"${toString modulesPath}/installer/netboot/netboot-minimal.nix"
@ -17,7 +23,10 @@ in {
system.build = rec {
kexec_tarball = maybe.mkForce (pkgs.callPackage "${toString modulesPath}/../lib/make-system-tarball.nix" {
storeContents = [
{ object = config.system.build.kexec_script; symlink = "/kexec_nixos"; }
{
object = config.system.build.kexec_script;
symlink = "/kexec_nixos";
}
];
contents = [];
});
@ -50,10 +59,11 @@ in {
boot.loader.grub.enable = false;
boot.kernelParams = [
"console=ttyS0,115200" # allows certain forms of remote access, if the hardware is setup right
"panic=30" "boot.panic_on_fail" # reboot the machine upon fatal boot issues
"console=ttyS0,115200" # allows certain forms of remote access, if the hardware is setup right
"panic=30"
"boot.panic_on_fail" # reboot the machine upon fatal boot issues
];
systemd.services.sshd.wantedBy = lib.mkForce [ "multi-user.target" ];
systemd.services.sshd.wantedBy = lib.mkForce ["multi-user.target"];
networking.hostName = lib.mkDefault "kexec";
formatAttr = "kexec_tarball";

View File

@ -1,5 +1,4 @@
{ modulesPath, ... }:
{
{modulesPath, ...}: {
imports = [
"${toString modulesPath}/virtualisation/kubevirt.nix"
];

View File

@ -1,5 +1,10 @@
{ config, lib, pkgs, modulesPath, ... }:
{
config,
lib,
pkgs,
modulesPath,
...
}: {
imports = [
"${toString modulesPath}/profiles/qemu-guest.nix"
];
@ -23,7 +28,7 @@
autoResize = true;
};
swapDevices = [{ device = "/dev/sdb"; }];
swapDevices = [{device = "/dev/sdb";}];
# Enable LISH and Linode booting w/ GRUB
boot = {
@ -38,7 +43,7 @@
growPartition = true;
# Set up LISH serial connection:
kernelParams = [ "console=ttyS0,19200n8" ];
kernelParams = ["console=ttyS0,19200n8"];
loader = {
# Increase timeout to allow LISH connection:

View File

@ -1,6 +1,9 @@
{ config, pkgs, modulesPath, ... }:
{
config,
pkgs,
modulesPath,
...
}: {
imports = [
"${toString modulesPath}/virtualisation/lxc-container.nix"
];
@ -8,4 +11,3 @@
formatAttr = "metadata";
filename = "*/tarball/*.tar.xz";
}

View File

@ -1,6 +1,10 @@
{ config, pkgs, lib, modulesPath, ... }:
{
config,
pkgs,
lib,
modulesPath,
...
}: {
imports = [
"${toString modulesPath}/virtualisation/lxc-container.nix"
];

View File

@ -1,11 +1,17 @@
{ modulesPath, lib, ... }:
if lib.pathExists "${toString modulesPath}/../maintainers/scripts/openstack/nova-image.nix" then {
{
modulesPath,
lib,
...
}:
if lib.pathExists "${toString modulesPath}/../maintainers/scripts/openstack/nova-image.nix"
then {
imports = [
"${toString modulesPath}/../maintainers/scripts/openstack/nova-image.nix"
];
formatAttr = "novaImage";
} else {
}
else {
imports = [
"${toString modulesPath}/../maintainers/scripts/openstack/openstack-image.nix"
];

View File

@ -1,5 +1,4 @@
{ modulesPath, ... }:
{
{modulesPath, ...}: {
imports = [
"${toString modulesPath}/virtualisation/proxmox-lxc.nix"
];

View File

@ -1,5 +1,4 @@
{ modulesPath, ... }:
{
{modulesPath, ...}: {
imports = [
"${toString modulesPath}/virtualisation/proxmox-image.nix"
];

View File

@ -1,5 +1,10 @@
{ config, lib, pkgs, modulesPath, ... }:
{
config,
lib,
pkgs,
modulesPath,
...
}: {
# for virtio kernel drivers
imports = [
"${toString modulesPath}/profiles/qemu-guest.nix"
@ -12,17 +17,16 @@
};
boot.growPartition = true;
boot.kernelParams = [ "console=ttyS0" ];
boot.loader.grub.device = if (pkgs.stdenv.system == "x86_64-linux") then
(lib.mkDefault "/dev/vda")
else
(lib.mkDefault "nodev");
boot.kernelParams = ["console=ttyS0"];
boot.loader.grub.device =
if (pkgs.stdenv.system == "x86_64-linux")
then (lib.mkDefault "/dev/vda")
else (lib.mkDefault "nodev");
boot.loader.grub.efiSupport = lib.mkIf (pkgs.stdenv.system != "x86_64-linux") (lib.mkDefault true);
boot.loader.grub.efiInstallAsRemovable = lib.mkIf (pkgs.stdenv.system != "x86_64-linux") (lib.mkDefault true);
boot.loader.timeout = 0;
system.build.qcow = import "${toString modulesPath}/../lib/make-disk-image.nix" {
inherit lib config pkgs;
diskSize = 8192;

View File

@ -1,9 +1,14 @@
{ config, lib, options, pkgs, modulesPath, ... }:
let
inherit (import ../lib.nix { inherit lib options; }) maybe;
{
config,
lib,
options,
pkgs,
modulesPath,
...
}: let
inherit (import ../lib.nix {inherit lib options;}) maybe;
in {
imports = [ ./raw.nix ];
imports = [./raw.nix];
boot.loader.grub = {
device = "nodev";

View File

@ -1,5 +1,10 @@
{ config, lib, pkgs, modulesPath, ... }:
{
config,
lib,
pkgs,
modulesPath,
...
}: {
fileSystems."/" = {
device = "/dev/disk/by-label/nixos";
autoResize = true;
@ -8,13 +13,12 @@
boot = {
growPartition = true;
kernelParams = [ "console=ttyS0" ];
kernelParams = ["console=ttyS0"];
loader.grub.device = lib.mkDefault "/dev/vda";
loader.timeout = lib.mkDefault 0;
initrd.availableKernelModules = [ "uas" ];
initrd.availableKernelModules = ["uas"];
};
system.build.raw = import "${toString modulesPath}/../lib/make-disk-image.nix" {
inherit lib config pkgs;
diskSize = "auto";

View File

@ -1,5 +1,8 @@
{ config, modulesPath, ... }:
{
config,
modulesPath,
...
}: {
imports = [
"${toString modulesPath}/installer/sd-card/sd-image-aarch64-installer.nix"
];

View File

@ -1,5 +1,10 @@
{ config, lib, pkgs, modulesPath, ... }:
{
config,
lib,
pkgs,
modulesPath,
...
}: {
imports = [
"${toString modulesPath}/installer/sd-card/sd-image-aarch64.nix"
];

View File

@ -1,5 +1,4 @@
{ modulesPath, ... }:
{
{modulesPath, ...}: {
imports = [
"${toString modulesPath}/virtualisation/vagrant-virtualbox-image.nix"
];

View File

@ -1,5 +1,4 @@
{ modulesPath, ... }:
{
{modulesPath, ...}: {
imports = [
"${toString modulesPath}/virtualisation/virtualbox-image.nix"
];

View File

@ -1,5 +1,4 @@
{ modulesPath, ... }:
{
{modulesPath, ...}: {
imports = [
./vm.nix
];

View File

@ -1,5 +1,4 @@
{ pkgs, ... }:
let
{pkgs, ...}: let
resize = pkgs.writeScriptBin "resize" ''
if [ -e /dev/tty ]; then
old=$(stty -g)
@ -15,8 +14,8 @@ in {
./vm.nix
];
virtualisation.graphics = false;
virtualisation.qemu.options = [ "-serial mon:stdio" ];
virtualisation.qemu.options = ["-serial mon:stdio"];
environment.systemPackages = [ resize ];
environment.systemPackages = [resize];
environment.loginShellInit = "${resize}/bin/resize";
}

View File

@ -1,5 +1,4 @@
{ modulesPath, ... }:
{
{modulesPath, ...}: {
imports = [
"${toString modulesPath}/virtualisation/qemu-vm.nix"
];

View File

@ -1,5 +1,4 @@
{ modulesPath, ... }:
{
{modulesPath, ...}: {
imports = [
"${toString modulesPath}/virtualisation/vmware-image.nix"
];

View File

@ -1,22 +1,21 @@
{ nixpkgs ? <nixpkgs>
, configuration ? <nixos-config>
, system ? builtins.currentSystem
, formatConfig
, flakeUri ? null
, flakeAttr ? null
}:
let
{
nixpkgs ? <nixpkgs>,
configuration ? <nixos-config>,
system ? builtins.currentSystem,
formatConfig,
flakeUri ? null,
flakeAttr ? null,
}: let
module = import ./format-module.nix;
# Will only get evaluated when used, so no worries
flake = builtins.getFlake flakeUri;
flakeSystem = flake.outputs.packages."${system}".nixosConfigurations."${flakeAttr}" or flake.outputs.nixosConfigurations."${flakeAttr}";
in
if flakeUri != null then
if flakeUri != null
then
flakeSystem.extendModules {
modules = [ module formatConfig ];
modules = [module formatConfig];
}
else
import "${toString nixpkgs}/nixos/lib/eval-config.nix" {

View File

@ -1,3 +1,5 @@
(import (builtins.fetchTarball https://github.com/edolstra/flake-compat/archive/master.tar.gz) {
src = ./.;
}).shellNix.default
})
.shellNix
.default