nixos-generate-config: Add --root option

If this option is given, nixos-generate-config will write the
configuration to /etc/nixos under the given root, and only emit
fileSystems attributes for filesystems under the given root directory.

The typically use case is at installation time, where you can say:

  $ nixos-generate-config --root /mnt

which will create /mnt/etc/nixos/{configuration.nix,hardware-configuration.nix}.

Also add a --force flag to force overwriting configuration.nix.
This commit is contained in:
Eelco Dolstra 2013-10-13 15:44:50 +02:00
parent 3875dcdfeb
commit ca3725bfc9
1 changed files with 18 additions and 1 deletions

View File

@ -21,6 +21,8 @@ sub uniq {
# Process the command line.
my $outDir = "/etc/nixos";
my $rootDir = ""; # = /
my $force = 0;
for (my $n = 0; $n < scalar @ARGV; $n++) {
my $arg = $ARGV[$n];
@ -32,6 +34,15 @@ for (my $n = 0; $n < scalar @ARGV; $n++) {
$outDir = $ARGV[$n];
die "$0: --dir requires an argument\n" unless defined $outDir;
}
elsif ($arg eq "--root") {
$n++;
$rootDir = $ARGV[$n];
die "$0: --root requires an argument\n" unless defined $rootDir;
$rootDir =~ s/\/*$//; # remove trailing slashes
}
elsif ($arg eq "--force") {
$force = 1;
}
else {
die "$0: unrecognized argument $arg\n";
}
@ -232,6 +243,10 @@ foreach my $fs (read_file("/proc/self/mountinfo")) {
next unless -d $mountPoint;
my @mountOptions = split /,/, $fields[5];
next if !in($mountPoint, $rootDir);
$mountPoint = substr($mountPoint, length($rootDir)); # strip the root directory (e.g. /mnt)
$mountPoint = "/" if $mountPoint eq "";
# Skip special filesystems.
next if in($mountPoint, "/proc") || in($mountPoint, "/dev") || in($mountPoint, "/sys") || in($mountPoint, "/run");
@ -317,6 +332,8 @@ my $initrdAvailableKernelModules = toNixExpr(uniq @initrdAvailableKernelModules)
my $kernelModules = toNixExpr(uniq @kernelModules);
my $modulePackages = toNixExpr(uniq @modulePackages);
$outDir = "$rootDir$outDir";
my $fn = "$outDir/hardware-configuration.nix";
print STDERR "writing $fn...\n";
mkpath($outDir, 0, 0755);
@ -343,7 +360,7 @@ EOF
# Generate a basic configuration.nix, unless one already exists.
$fn = "$outDir/configuration.nix";
if (! -e $fn) {
if ($force || ! -e $fn) {
print STDERR "writing $fn...\n";
my $bootloaderConfig;