mirror of
https://github.com/nix-community/nixos-generators.git
synced 2024-10-26 19:51:41 +03:00
feat(all-formats.nix): add readme section and test
This commit is contained in:
parent
f4a79d08d7
commit
27c86ee228
59
README.md
59
README.md
@ -147,6 +147,65 @@ For more details on configuring `binfmt`, have a look at:
|
||||
Once you've run `nixos-rebuild` with these options,
|
||||
you can use the `--system` option to create images for other architectures.
|
||||
|
||||
## Using as a nixos-module
|
||||
|
||||
`nixos-generators` can be included as a `NixOS module` into your existing `configuration.nix` making all available formats available through `config.formats` and configurable through `config.formatConfigs`. New formats can be defined by adding a new entry like `config.formatConfigs.my-new-format = {config, ...}: {}`.
|
||||
|
||||
An example `flake.nix` demonstrating this approach is below.
|
||||
|
||||
images can be built from the same `configuration.nix` by running:
|
||||
|
||||
- `nix build .#vmware` or
|
||||
- `nix build .#my-custom-format` or
|
||||
- `nix build .#<any-other-format>`
|
||||
|
||||
```nix
|
||||
{
|
||||
inputs = {
|
||||
nixpkgs.url = "nixpkgs/nixos-unstable";
|
||||
nixos-generators = {
|
||||
url = "github:nix-community/nixos-generators";
|
||||
inputs.nixpkgs.follows = "nixpkgs";
|
||||
};
|
||||
};
|
||||
outputs = { self, nixpkgs, nixos-generators, ... }: {
|
||||
|
||||
# A single nixos config outputting multiple formats.
|
||||
# Alternatively put this in a configuration.nix.
|
||||
nixosModules.my-machine = {config, ...}: {
|
||||
imports = [
|
||||
nixos-generators.nixosModules.all-formats
|
||||
];
|
||||
|
||||
nixpkgs.hostPlatform = "x86_64-linux";
|
||||
|
||||
# customize an existing format
|
||||
formatConfigs.vmware = {config, ...}: {
|
||||
services.openssh.enable = true;
|
||||
};
|
||||
|
||||
# define a new format
|
||||
formatConfigs.my-custom-format = {config, modulesPath, ...}: {
|
||||
imports = ["${toString modulesPath}/installer/cd-dvd/installation-cd-base.nix"];
|
||||
formatAttr = "isoImage";
|
||||
filename = "*.iso";
|
||||
networking.wireless.networks = {
|
||||
# ...
|
||||
};
|
||||
};
|
||||
|
||||
# the evaluated machine
|
||||
nixosConfigurations.my-machine = nixpkgs.lib.nixosSystem {
|
||||
modules = [self.nixosModules.my-machine];
|
||||
};
|
||||
|
||||
# optionally re-expose all formats as packages
|
||||
packages.x86_64-linux =
|
||||
self.nixosConfigurations.my-machine.config.formats;
|
||||
};
|
||||
}
|
||||
```
|
||||
|
||||
## Using in a Flake
|
||||
|
||||
`nixos-generators` can be included as a `Flake` input and provides
|
||||
|
58
checks/test-all-formats-flake/flake.nix
Normal file
58
checks/test-all-formats-flake/flake.nix
Normal file
@ -0,0 +1,58 @@
|
||||
/*
|
||||
Tests using the all-formats module through a flake.
|
||||
- Tests if foramts can be customized.
|
||||
- Tests if new foramts can be added
|
||||
*/
|
||||
{
|
||||
inputs = {
|
||||
nixpkgs.url = "nixpkgs/nixos-unstable";
|
||||
nixos-generators = {
|
||||
url = "github:nix-community/nixos-generators";
|
||||
inputs.nixpkgs.follows = "nixpkgs";
|
||||
};
|
||||
};
|
||||
outputs = {
|
||||
self,
|
||||
nixpkgs,
|
||||
nixos-generators,
|
||||
...
|
||||
}: {
|
||||
nixosModules.my-machine = {config, ...}: {
|
||||
imports = [
|
||||
nixos-generators.nixosModules.all-formats
|
||||
];
|
||||
|
||||
nixpkgs.hostPlatform = "x86_64-linux";
|
||||
|
||||
# customize an existing format
|
||||
formatConfigs.vmware = {config, ...}: {
|
||||
services.openssh.enable = false;
|
||||
};
|
||||
|
||||
# define a new format
|
||||
formatConfigs.my-custom-format = {
|
||||
config,
|
||||
modulesPath,
|
||||
...
|
||||
}: {
|
||||
imports = ["${toString modulesPath}/installer/cd-dvd/installation-cd-base.nix"];
|
||||
formatAttr = "isoImage";
|
||||
filename = "*.iso";
|
||||
networking.wireless.networks = {
|
||||
# ...
|
||||
};
|
||||
};
|
||||
};
|
||||
|
||||
nixosConfigurations.my-machine = nixpkgs.lib.nixosSystem {
|
||||
modules = [self.nixosModules.my-machine];
|
||||
};
|
||||
|
||||
checks.x86_64-linux = {
|
||||
test-flake_vmware =
|
||||
self.nixosConfigurations.my-machine.config.formats.vmware;
|
||||
test-flake_my-custom-format =
|
||||
self.nixosConfigurations.my-machine.config.formats.my-custom-format;
|
||||
};
|
||||
};
|
||||
}
|
25
flake.nix
25
flake.nix
@ -11,9 +11,20 @@
|
||||
self,
|
||||
nixpkgs,
|
||||
nixlib,
|
||||
}: let
|
||||
} @ inputs: let
|
||||
lib = nixpkgs.lib;
|
||||
|
||||
callFlake = flake: let
|
||||
args =
|
||||
inputs
|
||||
// {
|
||||
nixos-generators = self;
|
||||
self = subFlake;
|
||||
};
|
||||
subFlake = (import flake).outputs args;
|
||||
in
|
||||
subFlake;
|
||||
|
||||
# Ensures a derivation's name can be accessed without evaluating it deeply.
|
||||
# Prevents `nix flake show` from being very slow.
|
||||
makeLazyDrv = name: drv: {
|
||||
@ -30,11 +41,15 @@
|
||||
# Library modules (depend on nixlib)
|
||||
{
|
||||
# export all generator formats in ./formats
|
||||
nixosModules = nixlib.lib.mapAttrs' (file: _: {
|
||||
nixosModules =
|
||||
{
|
||||
all-formats = ./all-formats.nix;
|
||||
}
|
||||
// (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);
|
||||
}) (builtins.readDir ./formats));
|
||||
|
||||
# example usage in flakes:
|
||||
# outputs = { self, nixpkgs, nixos-generators, ...}: {
|
||||
@ -124,6 +139,9 @@
|
||||
});
|
||||
|
||||
checks =
|
||||
lib.recursiveUpdate
|
||||
(callFlake ./checks/test-all-formats-flake/flake.nix).checks
|
||||
(
|
||||
lib.genAttrs ["x86_64-linux" "aarch64-linux"]
|
||||
(
|
||||
system: let
|
||||
@ -149,6 +167,7 @@
|
||||
}
|
||||
// allFormats
|
||||
)
|
||||
)
|
||||
);
|
||||
|
||||
devShells = forAllSystems (system: let
|
||||
|
Loading…
Reference in New Issue
Block a user