Install NixOS from existing Linux

Hi,
thanks for the tips, but I still have some doubts.

I tried installing NixOS from liveusb but failed, so I’m trying to install it to another partition side-to-side with my current installation (Fedora 31).

My major problem is that I’m not sure how to mount the boot partitions (EFI system, GPT). In the current Fedora installation, the situation is this:

  • /dev/sda1 mounted in /boot/efi
  • /dev/sda2 mounted in /boot
  • /dev/sda3 is a LVM physical volume with 3 logical volumes:
  • /dev/VolumeGroup/swap
  • /dev/VolumeGroup/root mounted to / in fedora
  • /dev/VolumeGroup/nixos that should be mounted as / in nixos

The objective is to have nixos and fedora side-to-side, selectable in GRUB.

I prepared the /dev/VolumeGroup/nixos partition, but I’m not sure if I should mount the boot partitions as fedora is doing or not. Before installing, should I run

# mount /dev/VolumeGroup/nixos /mnt
# mount /dev/sda2 /mnt/boot
# mount /dev/sda1 /mnt/boot/efi

or should I run

# mount /dev/VolumeGroup/nixos /mnt
# mount /dev/sda1 /mnt/boot

Which one? I don’t know what the installer would do, so I can’t chose and I would like to get it right and not break the current system.

Also, I am not sure about the GRUB configuration. My current setup is:

  boot.loader.grub.useOSProber = true;

but I’m unsure if this is enough or I should have explicitly

 boot.loader.grub.extraEntries = ''
   menuentry "Fedora" {
     search --set=fedora --fs-uid dm-name-VolumeGroup-root
     configfile "($fedora)/boot/grub/grub.cfg"
   }
 '';

How should I proceed?
Thank you very much

@akiross I have (almost) the exact same setup (on lvm). This is the pertinent component of my configuration.nix:

{ config, pkgs, ... }:
{
  imports =
    [ # Include the results of the hardware scan.
      ./hardware-configuration.nix
    ];

  # Use the systemd-boot EFI boot loader.
  boot.loader.systemd-boot.enable = false;
  boot.loader.efi.canTouchEfiVariables = true;
  boot.loader.efi.efiSysMountPoint = "/boot/efi"; #This is the most important line
  boot.loader.grub.enable = true;
  boot.loader.grub.version = 2;
  boot.loader.grub.device = "nodev";
  boot.loader.grub.efiSupport = true;
  boot.loader.grub.useOSProber = false;
  boot.supportedFilesystems = [ "ntfs" ];
  boot.loader.grub.extraEntries = ''
	menuentry "Windows Boot Manager (on /dev/nvme0n1p2)" --class windows --class os {
		insmod part_gpt
		insmod fat
		search --no-floppy --fs-uuid --set=root 40E2-A3BF
		chainloader /EFI/Microsoft/Boot/bootmgfw.efi
	}

	menuentry "Fedora" --class fedora --class os {
		insmod part_gpt
		insmod ext2
		insmod fat
		search --no-floppy --fs-uuid --set=root 40E2-A3BF
		chainloader /EFI/fedora/grubx64.efi
	}
'';

}

And my hardware-configuration.nix:

# Do not modify this file!  It was generated by ‘nixos-generate-config’
# and may be overwritten by future invocations.  Please make changes
# to /etc/nixos/configuration.nix instead.
{ config, lib, pkgs, ... }:

{
  imports =
    [ <nixpkgs/nixos/modules/installer/scan/not-detected.nix>
    ];

  boot.initrd.availableKernelModules = [ "xhci_pci" "ahci" "nvme" "usb_storage" "usbhid" "sd_mod" "sr_mod" ];
  boot.initrd.kernelModules = [ "dm-snapshot" ];
  boot.kernelModules = [ "kvm-intel" ];
  boot.extraModulePackages = [ ];

  fileSystems."/" =
    { device = "/dev/disk/by-uuid/3244183c-5adf-47c9-8b20-6be2d3f45502";
      fsType = "ext4";
    };

  fileSystems."/home" =
    { device = "/dev/disk/by-uuid/b792912b-943e-48d4-a2ff-c716ef4123fc";
      fsType = "ext4";
    };

  fileSystems."/boot" =
    { device = "/dev/disk/by-uuid/d391084d-b279-4b51-a8ab-7ee9883d6b7d";
      fsType = "ext4";
    };

  fileSystems."/boot/efi" =
    { device = "/dev/disk/by-uuid/40E2-A3BF";
      fsType = "vfat";
    };

  swapDevices =
    [ { device = "/dev/disk/by-uuid/daddec15-1691-4efc-b783-9d683b44a05c"; }
    ];

  nix.maxJobs = lib.mkDefault 8;
  powerManagement.cpuFreqGovernor = lib.mkDefault "performance";
}

The confusing part is Fedora on UEFI has two partitions mounted under boot, one for the EFI images that should be mounted at /boot/efi (where Fedora’ grub is contained in /boot/efi/EFI/fedora/grubx64.cfg) and one partition for /boot which contains the kernel. T

The above should allow the system to boot. Change your uuid’s of course (include --root in the grub override). Note, this chainload’s Fedora’s grub, so you’ll be forwarded from the nix grub into fedora grub. I find this is more robust (although if anyone’s gotten auto-updating fedora kernels in nix grub please let me know.

See also: NixOS managed grub not detecting another distribution (Fedora)

1 Like

Hello @mjlbach and thanks for your help!

Your configuration has been useful, but I did something wrong because I am unable to use GRUB to start the Fedora system.

The current status is:

  1. power on
  2. grub showing
  3. fedora entries won’t work
  4. nixos entry work

NixOS is the default UEFI boot entry, so it will start, leading to grub, but I can still start Fedora via the UEFI boot menu - not grub.

I tried these configurations:

{
  imports =
    [ # Include the results of the hardware scan.
      ./hardware-configuration.nix
    ];

  boot.loader.systemd-boot.enable = false;
  boot.loader.efi.canTouchEfiVariables = true; 
  boot.loader.efi.efiSysMountPoint = "/boot/efi";
  boot.loader.grub.device = "nodev";  # do not install grub, just add entries
  boot.loader.grub.efiSupport = true;  # build grub with efi support, default false
  boot.loader.grub.useOSProber = false;
  boot.loader.grub.extraEntries = ''
    menuentry "Fedora ONE" {
        search --set=boot --fs-uid 8FBB-25E7
        configfile "($boot)/EFI/fedora/grub.cfg"
    }

    menuentry "Fedora TWO" --class fedora --class os {
        insmod part_gpt
        insmod ext2
        insmod fat
        search --no-floppy --fs-uuid --set=root 65176797-2614-4b29-be42-a529e26013b0
        search --no-floppy --fs-uuid --set=boot 8FBB-25E7
        chainloader ($boot)/EFI/fedora/grubx64.efi
    }
  '';
{ config, lib, pkgs, ... }:
 
{
  imports =
    [ <nixpkgs/nixos/modules/installer/scan/not-detected.nix>
    ];
 
  boot.initrd.availableKernelModules = [ "ahci" "nvme" "rtsx_pci_sdmmc" ];
  boot.initrd.kernelModules = [ "dm-snapshot" ];
  boot.kernelModules = [ "kvm-intel" ];
  boot.extraModulePackages = [ ];
 
  fileSystems."/" =
    { device = "/dev/disk/by-uuid/517592fb-a3a5-4338-9bd3-e1fcbc005c1e";
      fsType = "ext4";
    };
 
  fileSystems."/boot" =
    { device = "/dev/disk/by-uuid/65176797-2614-4b29-be42-a529e26013b0";
      fsType = "ext4";
    };
 
  fileSystems."/boot/efi" =
    { device = "/dev/disk/by-uuid/8FBB-25E7";
      fsType = "vfat";
    };
 
  swapDevices =
    [ { device = "/dev/disk/by-uuid/3db6a809-03c2-4d96-b870-51d6924581aa"; }
    ];
 
  powerManagement.cpuFreqGovernor = lib.mkDefault "powersave";
  # High-DPI console
  console.font = lib.mkDefault "${pkgs.terminus_font}/share/consolefonts/ter-u28n.psf.gz";
}

I added 2 entries just to see the various results.

In the first case, “Fedora ONE” (configfile), the system just reboots,
while in the second case “Fedora TWO” (chainloader), I get a grub> prompt, but I couldn’t manage to use it.

Not sure how to fix this, even if I can boot, so it’s not really a problem.

Did you try in your first config this:

	menuentry "Fedora" --class fedora --class os {
		insmod part_gpt
		insmod ext2
		insmod fat
		search --no-floppy --fs-uuid --set=root 8FBB-25E7
		chainloader /EFI/fedora/grubx64.efi
	}

Also can you confirm that grubx64.efi is located at /EFI/fedora/grubx64.efi when 8FBB-25E7 is mounted?

You may need to not set boot.loader.grub.device = “nodev”;

Before I got it working, I also had the issue where selecting fedora would trigger a restart.

Yes, I also tried that, but without prefixing ($root) to the chainload path I get a not found error.

Regarding the nodev: what’s the point of it? I get that nodev will change grub config, without installing grub. From my understanding, grub is already there due to fedora installation, so nix is updating the config but the grub I see is the one from fedora, so I don’t really need to unset nodev.
This being said, unsetting it will probably reinstall it over the existing one, which is not really a problem.

Is that correct?

Now, I thought I might try with no grub altogether, so I’ll just use the UEFI boot menu instead :slight_smile:
Not really a fix, but still good for me.

@wmertens May I ask if you could help me? I’d like to try your technique but I’ve not worked out how to get the install scripts…

Background is that the NixOS .iso’s using dd will not boot on my Thinkpad X1 Tablet 2nd gen. Ubuntu ones and others do… and I’ve done it 100s of times before so that’s odd.

Our approach is currently to just use kexec to boot into a proper nixos using this tool

2 Likes

Sorry for missing this! If the ISO doesn’t work that’s definitely an issue that should be tracked. In any case, the kexec approach works well. It’s reasonably straightforward, you first pass it the command line arguments and then you tell it to switch, at which point it instantly loads the kernel and it’s as if you booted regularly.

This thread was a great help to me. I think nixos-remote and Colmena are on my list to learn more, what I have done to get me going on bare metal is this:

However, for someone new to Nix but familiar with an existing distribution and happy their hardware-configuration might like to start installing their known linux and install NixOS from there.

Has someone had success using nixos-infect script or fork of it to install Nixos from a Asahi Apple Silicon install?