lol

nixos-generate-config: add --flake option (#383033)

authored by

Jörg Thalheim and committed by
GitHub
cdf83e0f 0add0231

+84 -4
+11
nixos/doc/manual/installation/installing.chapter.md
··· 398 398 [](#ch-options). A minimal example is shown in 399 399 [Example: NixOS Configuration](#ex-config). 400 400 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 + 401 404 The command `nixos-generate-config` can generate an initial 402 405 configuration file for you: 403 406 ··· 489 492 other issue (such as a network outage while downloading binaries 490 493 from the NixOS binary cache), you can re-run `nixos-install` after 491 494 fixing your `configuration.nix`. 495 + 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 + ``` 492 503 493 504 As the last step, `nixos-install` will ask you to set the password 494 505 for the `root` user, e.g.
+2
nixos/doc/manual/release-notes/rl-2505.section.md
··· 30 30 31 31 - `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. 32 32 33 + - 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. 34 + 33 35 - A `nixos-rebuild build-image` sub-command has been added. 34 36 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`. 35 37
+9 -1
nixos/modules/installer/tools/manpages/nixos-generate-config.8
··· 12 12 .Op Fl -force 13 13 .Op Fl -root Ar root 14 14 .Op Fl -dir Ar dir 15 + .Op Fl -flake 15 16 . 16 17 . 17 18 . ··· 68 69 .It Fl -force 69 70 Overwrite 70 71 .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 + \&. 72 80 . 73 81 .It Fl -no-filesystems 74 82 Omit everything concerning file systems and swap devices from the hardware configuration.
+17
nixos/modules/installer/tools/nixos-generate-config.pl
··· 35 35 my $rootDir = ""; # = / 36 36 my $force = 0; 37 37 my $noFilesystems = 0; 38 + my $flake = 0; 38 39 my $showHardwareConfig = 0; 39 40 40 41 for (my $n = 0; $n < scalar @ARGV; $n++) { ··· 63 64 } 64 65 elsif ($arg eq "--show-hardware-config") { 65 66 $showHardwareConfig = 1; 67 + } 68 + elsif ($arg eq "--flake") { 69 + $flake = 1; 66 70 } 67 71 else { 68 72 die "$0: unrecognized argument ‘$arg’\n"; ··· 660 664 print STDERR "writing $fn...\n"; 661 665 mkpath($outDir, 0, 0755); 662 666 write_file($fn, $hwConfig); 667 + 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 + } 663 680 664 681 # Generate a basic configuration.nix, unless one already exists. 665 682 $fn = "$outDir/configuration.nix";
+39 -3
nixos/modules/installer/tools/tools.nix
··· 1 1 # This module generates nixos-install, nixos-rebuild, 2 2 # nixos-generate-config, etc. 3 3 4 - { config, lib, pkgs, ... }: 4 + { config, lib, pkgs, options, ... }: 5 5 6 6 let 7 7 makeProg = args: pkgs.replaceVarsWith (args // { ··· 23 23 hostPlatformSystem = pkgs.stdenv.hostPlatform.system; 24 24 detectvirt = "${config.systemd.package}/bin/systemd-detect-virt"; 25 25 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; 27 27 xserverEnabled = config.services.xserver.enable; 28 28 }; 29 29 manPage = ./manpages/nixos-generate-config.8; ··· 54 54 withNgSuffix = false; 55 55 withReexec = true; 56 56 }; 57 + 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 + ''; 57 75 58 76 defaultConfigTemplate = '' 59 77 # Edit this configuration file to define what should be installed on ··· 176 194 in 177 195 { 178 196 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 + 179 215 configuration = lib.mkOption { 180 216 internal = true; 181 217 type = lib.types.str; ··· 196 232 desktopConfiguration = lib.mkOption { 197 233 internal = true; 198 234 type = lib.types.listOf lib.types.lines; 199 - default = []; 235 + default = [ ]; 200 236 description = '' 201 237 Text to preseed the desktop configuration that `nixos-generate-config` 202 238 saves to `/etc/nixos/configuration.nix`.
+6
nixos/tests/nixos-generate-config.nix
··· 26 26 machine.wait_for_unit("multi-user.target") 27 27 machine.succeed("nixos-generate-config") 28 28 29 + machine.succeed("nix-instantiate --parse /etc/nixos/configuration.nix /etc/nixos/hardware-configuration.nix") 30 + 29 31 # Test if the configuration really is overridden 30 32 machine.succeed("grep 'OVERRIDDEN' /etc/nixos/configuration.nix") 31 33 ··· 41 43 machine.succeed( 42 44 "grep 'services\\.xserver\\.desktopManager\\.gnome\\.enable = true;' /etc/nixos/configuration.nix" 43 45 ) 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") 44 50 ''; 45 51 } 46 52 )