nixos-generate-config: add --flake option

Co-authored-by: éclairevoyant <848000+eclairevoyant@users.noreply.github.com>
This commit is contained in:
Pablo Ovelleiro Corral 2025-02-12 09:04:17 +01:00 committed by Jörg Thalheim
parent 29c58cf78d
commit 1606ea91b4
6 changed files with 84 additions and 4 deletions

View file

@ -398,6 +398,9 @@ Use the following commands:
[](#ch-options). A minimal example is shown in
[Example: NixOS Configuration](#ex-config).
This command accepts an optional `--flake` option, to also generate a
`flake.nix` file, if you want to set up a flake-based configuration.
The command `nixos-generate-config` can generate an initial
configuration file for you:
@ -490,6 +493,14 @@ Use the following commands:
from the NixOS binary cache), you can re-run `nixos-install` after
fixing your `configuration.nix`.
If you opted for a flake-based configuration, you will need to pass the
`--flake` here as well and specify the name of the configuration as used in
the `flake.nix` file. For the default generated flake, this is `nixos`.
```ShellSession
# nixos-install --flake 'path/to/flake.nix#nixos'
```
As the last step, `nixos-install` will ask you to set the password
for the `root` user, e.g.

View file

@ -28,6 +28,8 @@
- `nixos-rebuild-ng`, a full rewrite of `nixos-rebuild` in Python, is available for testing. You can enable it by setting [system.rebuild.enableNg](options.html#opt-system.rebuild.enableNg) in your configuration (this will replace the old `nixos-rebuild`), or by adding `nixos-rebuild-ng` to your `environment.systemPackages` (in this case, it will live side-by-side with `nixos-rebuild` as `nixos-rebuild-ng`). It is expected that the next major version of NixOS (25.11) will enable `system.rebuild.enableNg` by default.
- The `nixos-generate-config` command now supports a optional `--flake` option, which will generate a flake.nix file alongside the `configuration.nix` and `hardware-configuration.nix`, providing an easy instroduction into flake-based system configurations.
- A `nixos-rebuild build-image` sub-command has been added.
It allows users to build platform-specific (disk) images from their NixOS configurations. `nixos-rebuild build-image` works similar to the popular [nix-community/nixos-generators](https://github.com/nix-community/nixos-generators) project. See new [section on image building in the nixpkgs manual](https://nixos.org/manual/nixpkgs/unstable/#sec-image-nixos-rebuild-build-image). It is also available for `nixos-rebuild-ng`.

View file

@ -12,6 +12,7 @@
.Op Fl -force
.Op Fl -root Ar root
.Op Fl -dir Ar dir
.Op Fl -flake
.
.
.
@ -68,7 +69,14 @@ instead of
.It Fl -force
Overwrite
.Pa /etc/nixos/configuration.nix
if it already exists.
(and
.Pa /etc/nixos/flake.nix
if --flake is passed) if already present.
.
.It Fl -flake
Also generate
.Pa /etc/nixos/flake.nix Ns
\&.
.
.It Fl -no-filesystems
Omit everything concerning file systems and swap devices from the hardware configuration.

View file

@ -35,6 +35,7 @@ my $outDir = "/etc/nixos";
my $rootDir = ""; # = /
my $force = 0;
my $noFilesystems = 0;
my $flake = 0;
my $showHardwareConfig = 0;
for (my $n = 0; $n < scalar @ARGV; $n++) {
@ -64,6 +65,9 @@ for (my $n = 0; $n < scalar @ARGV; $n++) {
elsif ($arg eq "--show-hardware-config") {
$showHardwareConfig = 1;
}
elsif ($arg eq "--flake") {
$flake = 1;
}
else {
die "$0: unrecognized argument $arg\n";
}
@ -661,6 +665,19 @@ if ($showHardwareConfig) {
mkpath($outDir, 0, 0755);
write_file($fn, $hwConfig);
$fn = "$outDir/flake.nix";
if ($flake) {
if ($force || ! -e $fn) {
print STDERR "writing $fn...\n";
mkpath($outDir, 0, 0755);
write_file($fn, <<EOF);
@flake@
EOF
} else {
print STDERR "warning: not overwriting existing $fn\n";
}
}
# Generate a basic configuration.nix, unless one already exists.
$fn = "$outDir/configuration.nix";
if ($force || ! -e $fn) {

View file

@ -1,7 +1,7 @@
# This module generates nixos-install, nixos-rebuild,
# nixos-generate-config, etc.
{ config, lib, pkgs, ... }:
{ config, lib, pkgs, options, ... }:
let
makeProg = args: pkgs.replaceVarsWith (args // {
@ -23,7 +23,7 @@ let
hostPlatformSystem = pkgs.stdenv.hostPlatform.system;
detectvirt = "${config.systemd.package}/bin/systemd-detect-virt";
btrfs = "${pkgs.btrfs-progs}/bin/btrfs";
inherit (config.system.nixos-generate-config) configuration desktopConfiguration;
inherit (config.system.nixos-generate-config) configuration desktopConfiguration flake;
xserverEnabled = config.services.xserver.enable;
};
manPage = ./manpages/nixos-generate-config.8;
@ -55,6 +55,24 @@ let
withReexec = true;
};
defaultFlakeTemplate = ''
{
inputs = {
# This is pointing to an unstable release.
# If you prefer a stable release instead, you can this to the latest number shown here: https://nixos.org/download
# i.e. nixos-24.11
# Use `nix flake update` to update the flake to the latest revision of the chosen release channel.
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
};
outputs = inputs\@{ self, nixpkgs, ... }: {
# NOTE: '${options.networking.hostName.default}' is the default hostname
nixosConfigurations.${options.networking.hostName.default} = nixpkgs.lib.nixosSystem {
modules = [ ./configuration.nix ];
};
};
}
'';
defaultConfigTemplate = ''
# Edit this configuration file to define what should be installed on
# your system. Help is available in the configuration.nix(5) man page, on
@ -176,6 +194,24 @@ let
in
{
options.system.nixos-generate-config = {
flake = lib.mkOption {
internal = true;
type = lib.types.str;
default = defaultFlakeTemplate;
description = ''
The NixOS module that `nixos-generate-config`
saves to `/etc/nixos/flake.nix` if --flake is set.
This is an internal option. No backward compatibility is guaranteed.
Use at your own risk!
Note that this string gets spliced into a Perl script. The perl
variable `$bootLoaderConfig` can be used to
splice in the boot loader configuration.
'';
};
configuration = lib.mkOption {
internal = true;
type = lib.types.str;
@ -196,7 +232,7 @@ in
desktopConfiguration = lib.mkOption {
internal = true;
type = lib.types.listOf lib.types.lines;
default = [];
default = [ ];
description = ''
Text to preseed the desktop configuration that `nixos-generate-config`
saves to `/etc/nixos/configuration.nix`.

View file

@ -26,6 +26,8 @@ import ./make-test-python.nix (
machine.wait_for_unit("multi-user.target")
machine.succeed("nixos-generate-config")
machine.succeed("nix-instantiate --parse /etc/nixos/configuration.nix /etc/nixos/hardware-configuration.nix")
# Test if the configuration really is overridden
machine.succeed("grep 'OVERRIDDEN' /etc/nixos/configuration.nix")
@ -41,6 +43,10 @@ import ./make-test-python.nix (
machine.succeed(
"grep 'services\\.xserver\\.desktopManager\\.gnome\\.enable = true;' /etc/nixos/configuration.nix"
)
machine.succeed("rm -rf /etc/nixos")
machine.succeed("nixos-generate-config --flake")
machine.succeed("nix-instantiate --parse /etc/nixos/flake.nix /etc/nixos/configuration.nix /etc/nixos/hardware-configuration.nix")
'';
}
)