@@ -8,6 +8,7 @@ use File::stat;
88use File::Copy;
99use File::Slurp;
1010use File::Temp;
11+use JSON;
1112require List::Compare;
1213use POSIX;
1314use Cwd;
@@ -606,9 +607,12 @@ struct(GrubState => {
606607 efi => '$',
607608 devices => '$',
608609 efiMountPoint => '$',
610+ extraGrubInstallArgs => '@',
609611});
612+# If you add something to the state file, only add it to the end
613+# because it is read line-by-line.
610614sub readGrubState {
611− my $defaultGrubState = GrubState->new(name => "", version => "", efi => "", devices => "", efiMountPoint => "" );
615+ my $defaultGrubState = GrubState->new(name => "", version => "", efi => "", devices => "", efiMountPoint => "", extraGrubInstallArgs => () );
612616 open FILE, "<$bootPath/grub/state" or return $defaultGrubState;
613617 local $/ = "\n";
614618 my $name = <FILE>;
@@ -621,16 +625,34 @@ sub readGrubState {
621625 chomp($devices);
622626 my $efiMountPoint = <FILE>;
623627 chomp($efiMountPoint);
628+ # Historically, arguments in the state file were one per each line, but that
629+ # gets really messy when newlines are involved, structured arguments
630+ # like lists are needed (they have to have a separator encoding), or even worse,
631+ # when we need to remove a setting in the future. Thus, the 6th line is a JSON
632+ # object that can store structured data, with named keys, and all new state
633+ # should go in there.
634+ my $jsonStateLine = <FILE>;
635+ # For historical reasons we do not check the values above for un-definedness
636+ # (that is, when the state file has too few lines and EOF is reached),
637+ # because the above come from the first version of this logic and are thus
638+ # guaranteed to be present.
639+ $jsonStateLine = defined $jsonStateLine ? $jsonStateLine : '{}'; # empty JSON object
640+ chomp($jsonStateLine);
641+ my %jsonState = %{decode_json($jsonStateLine)};
642+ my @extraGrubInstallArgs = @{$jsonState{'extraGrubInstallArgs'}};
624643 close FILE;
625− my $grubState = GrubState->new(name => $name, version => $version, efi => $efi, devices => $devices, efiMountPoint => $efiMountPoint );
644+ my $grubState = GrubState->new(name => $name, version => $version, efi => $efi, devices => $devices, efiMountPoint => $efiMountPoint, extraGrubInstallArgs => \@extraGrubInstallArgs );
626645 return $grubState
627646}
628647
629648my @deviceTargets = getList('devices');
630649my $prevGrubState = readGrubState();
631650my @prevDeviceTargets = split/,/, $prevGrubState->devices;
651+my @extraGrubInstallArgs = getList('extraGrubInstallArgs');
652+my @prevExtraGrubInstallArgs = $prevGrubState->extraGrubInstallArgs;
632653
633654my $devicesDiffer = scalar (List::Compare->new( '-u', '-a', \@deviceTargets, \@prevDeviceTargets)->get_symmetric_difference());
655+my $extraGrubInstallArgsDiffer = scalar (List::Compare->new( '-u', '-a', \@extraGrubInstallArgs, \@prevExtraGrubInstallArgs)->get_symmetric_difference());
634656my $nameDiffer = get("fullName") ne $prevGrubState->name;
635657my $versionDiffer = get("fullVersion") ne $prevGrubState->version;
636658my $efiDiffer = $efiTarget ne $prevGrubState->efi;
@@ -639,7 +661,7 @@ if (($ENV{'NIXOS_INSTALL_GRUB'} // "") eq "1") {
639661 warn "NIXOS_INSTALL_GRUB env var deprecated, use NIXOS_INSTALL_BOOTLOADER";
640662 $ENV{'NIXOS_INSTALL_BOOTLOADER'} = "1";
641663}
642−my $requireNewInstall = $devicesDiffer || $nameDiffer || $versionDiffer || $efiDiffer || $efiMountPointDiffer || (($ENV{'NIXOS_INSTALL_BOOTLOADER'} // "") eq "1");
664+my $requireNewInstall = $devicesDiffer || $extraGrubInstallArgsDiffer || $nameDiffer || $versionDiffer || $efiDiffer || $efiMountPointDiffer || (($ENV{'NIXOS_INSTALL_BOOTLOADER'} // "") eq "1");
643665
644666# install a symlink so that grub can detect the boot drive
645667my $tmpDir = File::Temp::tempdir(CLEANUP => 1) or die "Failed to create temporary space";
@@ -650,7 +672,7 @@ if (($requireNewInstall != 0) && ($efiTarget eq "no" || $efiTarget eq "both")) {
650672 foreach my $dev (@deviceTargets) {
651673 next if $dev eq "nodev";
652674 print STDERR "installing the GRUB $grubVersion boot loader on $dev...\n";
653− my @command = ("$grub/sbin/grub-install", "--recheck", "--root-directory=$tmpDir", Cwd::abs_path($dev));
675+ my @command = ("$grub/sbin/grub-install", "--recheck", "--root-directory=$tmpDir", Cwd::abs_path($dev), @extraGrubInstallArgs);
654676 if ($forceInstall eq "true") {
655677 push @command, "--force";
656678 }
@@ -665,7 +687,7 @@ if (($requireNewInstall != 0) && ($efiTarget eq "no" || $efiTarget eq "both")) {
665687# install EFI GRUB
666688if (($requireNewInstall != 0) && ($efiTarget eq "only" || $efiTarget eq "both")) {
667689 print STDERR "installing the GRUB $grubVersion EFI boot loader into $efiSysMountPoint...\n";
668− my @command = ("$grubEfi/sbin/grub-install", "--recheck", "--target=$grubTargetEfi", "--boot-directory=$bootPath", "--efi-directory=$efiSysMountPoint");
690+ my @command = ("$grubEfi/sbin/grub-install", "--recheck", "--target=$grubTargetEfi", "--boot-directory=$bootPath", "--efi-directory=$efiSysMountPoint", @extraGrubInstallArgs);
669691 if ($forceInstall eq "true") {
670692 push @command, "--force";
671693 }
@@ -692,6 +714,11 @@ if ($requireNewInstall != 0) {
692714 print FILE $efiTarget, "\n" or die;
693715 print FILE join( ",", @deviceTargets ), "\n" or die;
694716 print FILE $efiSysMountPoint, "\n" or die;
717+ my %jsonState = (
718+ extraGrubInstallArgs => \@extraGrubInstallArgs
719+ );
720+ my $jsonStateLine = encode_json(\%jsonState);
721+ print FILE $jsonStateLine, "\n" or die;
695722 close FILE or die;
696723
697724 # Atomically switch to the new state file