Skip to content

Commit 1606ea9

Browse files
pinpoxeclairevoyant
authored andcommitted
nixos-generate-config: add --flake option
Co-authored-by: éclairevoyant <[email protected]>
1 parent 29c58cf commit 1606ea9

File tree

6 files changed

+84
-4
lines changed

6 files changed

+84
-4
lines changed

nixos/doc/manual/installation/installing.chapter.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,9 @@ Use the following commands:
398398
[](#ch-options). A minimal example is shown in
399399
[Example: NixOS Configuration](#ex-config).
400400

401+
This command accepts an optional `--flake` option, to also generate a
402+
`flake.nix` file, if you want to set up a flake-based configuration.
403+
401404
The command `nixos-generate-config` can generate an initial
402405
configuration file for you:
403406

@@ -490,6 +493,14 @@ Use the following commands:
490493
from the NixOS binary cache), you can re-run `nixos-install` after
491494
fixing your `configuration.nix`.
492495

496+
If you opted for a flake-based configuration, you will need to pass the
497+
`--flake` here as well and specify the name of the configuration as used in
498+
the `flake.nix` file. For the default generated flake, this is `nixos`.
499+
500+
```ShellSession
501+
# nixos-install --flake 'path/to/flake.nix#nixos'
502+
```
503+
493504
As the last step, `nixos-install` will ask you to set the password
494505
for the `root` user, e.g.
495506

nixos/doc/manual/release-notes/rl-2505.section.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828

2929
- `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.
3030

31+
- 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.
32+
3133
- A `nixos-rebuild build-image` sub-command has been added.
3234
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`.
3335

nixos/modules/installer/tools/manpages/nixos-generate-config.8

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
.Op Fl -force
1313
.Op Fl -root Ar root
1414
.Op Fl -dir Ar dir
15+
.Op Fl -flake
1516
.
1617
.
1718
.
@@ -68,7 +69,14 @@ instead of
6869
.It Fl -force
6970
Overwrite
7071
.Pa /etc/nixos/configuration.nix
71-
if it already exists.
72+
(and
73+
.Pa /etc/nixos/flake.nix
74+
if --flake is passed) if already present.
75+
.
76+
.It Fl -flake
77+
Also generate
78+
.Pa /etc/nixos/flake.nix Ns
79+
\&.
7280
.
7381
.It Fl -no-filesystems
7482
Omit everything concerning file systems and swap devices from the hardware configuration.

nixos/modules/installer/tools/nixos-generate-config.pl

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ sub runCommand {
3535
my $rootDir = ""; # = /
3636
my $force = 0;
3737
my $noFilesystems = 0;
38+
my $flake = 0;
3839
my $showHardwareConfig = 0;
3940

4041
for (my $n = 0; $n < scalar @ARGV; $n++) {
@@ -64,6 +65,9 @@ sub runCommand {
6465
elsif ($arg eq "--show-hardware-config") {
6566
$showHardwareConfig = 1;
6667
}
68+
elsif ($arg eq "--flake") {
69+
$flake = 1;
70+
}
6771
else {
6872
die "$0: unrecognized argument ‘$arg\n";
6973
}
@@ -661,6 +665,19 @@ sub generateXserverConfig {
661665
mkpath($outDir, 0, 0755);
662666
write_file($fn, $hwConfig);
663667

668+
$fn = "$outDir/flake.nix";
669+
if ($flake) {
670+
if ($force || ! -e $fn) {
671+
print STDERR "writing $fn...\n";
672+
mkpath($outDir, 0, 0755);
673+
write_file($fn, <<EOF);
674+
@flake@
675+
EOF
676+
} else {
677+
print STDERR "warning: not overwriting existing $fn\n";
678+
}
679+
}
680+
664681
# Generate a basic configuration.nix, unless one already exists.
665682
$fn = "$outDir/configuration.nix";
666683
if ($force || ! -e $fn) {

nixos/modules/installer/tools/tools.nix

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# This module generates nixos-install, nixos-rebuild,
22
# nixos-generate-config, etc.
33

4-
{ config, lib, pkgs, ... }:
4+
{ config, lib, pkgs, options, ... }:
55

66
let
77
makeProg = args: pkgs.replaceVarsWith (args // {
@@ -23,7 +23,7 @@ let
2323
hostPlatformSystem = pkgs.stdenv.hostPlatform.system;
2424
detectvirt = "${config.systemd.package}/bin/systemd-detect-virt";
2525
btrfs = "${pkgs.btrfs-progs}/bin/btrfs";
26-
inherit (config.system.nixos-generate-config) configuration desktopConfiguration;
26+
inherit (config.system.nixos-generate-config) configuration desktopConfiguration flake;
2727
xserverEnabled = config.services.xserver.enable;
2828
};
2929
manPage = ./manpages/nixos-generate-config.8;
@@ -55,6 +55,24 @@ let
5555
withReexec = true;
5656
};
5757

58+
defaultFlakeTemplate = ''
59+
{
60+
inputs = {
61+
# This is pointing to an unstable release.
62+
# If you prefer a stable release instead, you can this to the latest number shown here: https://nixos.org/download
63+
# i.e. nixos-24.11
64+
# Use `nix flake update` to update the flake to the latest revision of the chosen release channel.
65+
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
66+
};
67+
outputs = inputs\@{ self, nixpkgs, ... }: {
68+
# NOTE: '${options.networking.hostName.default}' is the default hostname
69+
nixosConfigurations.${options.networking.hostName.default} = nixpkgs.lib.nixosSystem {
70+
modules = [ ./configuration.nix ];
71+
};
72+
};
73+
}
74+
'';
75+
5876
defaultConfigTemplate = ''
5977
# Edit this configuration file to define what should be installed on
6078
# your system. Help is available in the configuration.nix(5) man page, on
@@ -176,6 +194,24 @@ let
176194
in
177195
{
178196
options.system.nixos-generate-config = {
197+
198+
flake = lib.mkOption {
199+
internal = true;
200+
type = lib.types.str;
201+
default = defaultFlakeTemplate;
202+
description = ''
203+
The NixOS module that `nixos-generate-config`
204+
saves to `/etc/nixos/flake.nix` if --flake is set.
205+
206+
This is an internal option. No backward compatibility is guaranteed.
207+
Use at your own risk!
208+
209+
Note that this string gets spliced into a Perl script. The perl
210+
variable `$bootLoaderConfig` can be used to
211+
splice in the boot loader configuration.
212+
'';
213+
};
214+
179215
configuration = lib.mkOption {
180216
internal = true;
181217
type = lib.types.str;
@@ -196,7 +232,7 @@ in
196232
desktopConfiguration = lib.mkOption {
197233
internal = true;
198234
type = lib.types.listOf lib.types.lines;
199-
default = [];
235+
default = [ ];
200236
description = ''
201237
Text to preseed the desktop configuration that `nixos-generate-config`
202238
saves to `/etc/nixos/configuration.nix`.

nixos/tests/nixos-generate-config.nix

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ import ./make-test-python.nix (
2626
machine.wait_for_unit("multi-user.target")
2727
machine.succeed("nixos-generate-config")
2828
29+
machine.succeed("nix-instantiate --parse /etc/nixos/configuration.nix /etc/nixos/hardware-configuration.nix")
30+
2931
# Test if the configuration really is overridden
3032
machine.succeed("grep 'OVERRIDDEN' /etc/nixos/configuration.nix")
3133
@@ -41,6 +43,10 @@ import ./make-test-python.nix (
4143
machine.succeed(
4244
"grep 'services\\.xserver\\.desktopManager\\.gnome\\.enable = true;' /etc/nixos/configuration.nix"
4345
)
46+
47+
machine.succeed("rm -rf /etc/nixos")
48+
machine.succeed("nixos-generate-config --flake")
49+
machine.succeed("nix-instantiate --parse /etc/nixos/flake.nix /etc/nixos/configuration.nix /etc/nixos/hardware-configuration.nix")
4450
'';
4551
}
4652
)

0 commit comments

Comments
 (0)