4

It is possible to install packages from nixos-unstable in /etc/nixos/configuration.nix using the configuration from this answer.

Here is an example of installing the htop packages from nixos-unstable:

{ config, pkgs, ... }:

let
  unstableTarball =
    fetchTarball
      https://github.com/NixOS/nixpkgs-channels/archive/nixos-unstable.tar.gz;
in
{
  ...

  nixpkgs.config = {
    packageOverrides = pkgs: {
      unstable = import unstableTarball {
        config = config.nixpkgs.config;
      };
    };
  };

  environment.systemPackages = with pkgs; [
    ...
    unstable.htop
  ];

  ...
};

I would like to be able to install the Virtualbox package (and related kernel modules) from nixos-unstable as well.

Naively adding the virtualbox package to environment.systemPackages doesn't work like I expect it would. The Virtualbox modules matching the unstable version of Virtualbox do not get installed. Here is a snippet from my /etc/nixos/configuration.nix:

  nixpkgs.config.virtualbox.enableExtensionPack = true;
  virtualisation.virtualbox.host.enable = true;
  environment.systemPackages = with pkgs; [
    ...
    unstable.virtualbox
  ];

The above will correctly install the virtualbox package from nixos-unstable, but not the Virtualbox kernel modules.

How can I get the Virtualbox kernel modules to be installed from nixos-unstable as well? And why doesn't the above work?

illabout
  • 3,517
  • 1
  • 18
  • 39
  • I posted a question about this on the nixpkgs github: https://github.com/NixOS/nixpkgs/issues/35440 – illabout Mar 01 '18 at 12:03
  • It looks like there was a response on the github issue showing how to do this (however I haven't personally tested this out yet): https://github.com/NixOS/nixpkgs/issues/35440#issuecomment-389321234 – illabout May 19 '18 at 00:48
  • I tried the solution in [the GitHub issue comment](https://github.com/NixOS/nixpkgs/issues/35440#issuecomment-389321234), but it didn't work on NixOS 22.11. See [my reply](https://github.com/NixOS/nixpkgs/issues/35440#issuecomment-1519571252) for details. – akaihola Apr 24 '23 at 08:00

2 Answers2

1

Your configuration does not work, because the virtualbox module has its own reference to the virtualbox package. Perhaps its should expose an option to override the package like some other modules do, but for now it doesn't. It shouldn't be hard to make a pull request for it.

The alternative is to replace the offending module/modules by disabling the using disabledModules and then importing your replacements with imports.

Either way, your mileage may vary. The first option seems to be the cleanest to me, but you may want to check the differences between the nixos modules in your release and unstable versions.

Robert Hensing
  • 6,708
  • 18
  • 23
  • Would you be able to update your answer with how to replace the virtualbox modules? The link you provided says to add a field like the following: `imports = [ ];`. However, in my configuration above, I don't have `nixos-unstable` in my `NIX_PATH`, but instead in a let-bound variable. How can I add a module from my `unstableTarball` to the `import` list? – illabout Feb 17 '18 at 15:51
  • 1
    You can import from a variable like this: `imports = [ (unstableTarball + "/nixos/modules/services/databases/postgresql.nix") ]`. Note the distinction between `import`, a primitive function from the Nix language and `imports`, an attribute name used by the NixOS module system. – Robert Hensing Feb 18 '18 at 15:30
  • Putting the `virtualbox` module in `disabledModules` while also adding it to `imports` doesn't appear to work. Here's a snippet of my `configuration.nix`: http://nixpaste.lbr.uno/H1bGSJvE?nix. The kernel modules for virtualbox do not appear to getting rebuilt. Maybe the only way is your first suggestion: expose an option to override the package? – illabout Feb 20 '18 at 15:07
  • 1
    That still seems like an appropriate option. What might also work is to override virtualbox in your `nixpkgs.config.packageOverrides` or, better, `nixpkgs.overlays`. There's some [funny business going on in the host module](https://github.com/NixOS/nixpkgs/blob/2cafea200cf855e8119b2f2afa36101bf366eccd/nixos/modules/virtualisation/virtualbox-host.nix#L8-L15) that just might inject the proper virtualbox sources into the kernel configuration. – Robert Hensing Feb 21 '18 at 12:19
  • That seems to work a little better, but I am still getting an error: http://nixpaste.lbr.uno/15ayA-aV?nix. When trying to override virtualbox in `nixpkgs.config.packageOverrides`, it appears that the virtualbox kernel modules are no longer able to find the kernel headers. Any clue on what could be going on here? Or how to fix it? – illabout Feb 22 '18 at 15:22
1

The original kernel module is installed, because it is built separately, against a specific kernel. Normally, the virtualbox-host module keeps the kernel module and the user program in sync.

When you want to override the user program, you would also need to override the kernel module. This would amount to something like this:

!!untested code!!

  ...

  boot.kernelPackages = pkgs.linuxPackages.extend (self: super: {
    virtualbox = super.virtualbox.override {
      inherit (self) kernel;
    };
    virtualboxGuestAdditions = super.virtualboxGuestAdditions.override {
      inherit (self) kernel;
    };
  });
  ## also, the user program override should be introduced 
  ## directly at packageOverrides
  nixpkgs.config.packageOverrides = pkgs: rec {
    unstable = import
      (fetchTarball https://github.com/NixOS/nixpkgs-channels/archive/nixos-unstable.tar.gz) {
        config = config.nixpkgs.config;
      };
    virtualbox = unstable.virtualbox;
  };

  ...

Something like this should allow you to run the regular virtualbox-host module, with the unstable packages injected.

Bendlas
  • 787
  • 3
  • 12
  • That said, and having run a mixed stable / unstable system myself in the past, I can tell you right now, that you'll probably be better off, just running unstable, anyway :-) – Bendlas Mar 08 '18 at 23:30
  • This case of virtualbox might work, though, since the kernel is an awesomely stable target. – Bendlas Mar 08 '18 at 23:34
  • 1
    Thanks for the suggestion. However it appears that this causes the same error as described in the [issue](https://github.com/NixOS/nixpkgs/issues/35440) on Github: `/tmp/nix-build-virtualbox-modules-5.2.6-4.9.78.drv-0/virtualbox-5.2.6-modsrc/vboxdrv/Makefile.include.header:141: *** Error: unable to find the headers of the Linux kernel to build against. Specify KERN_VER= (currently 4.9.78) and run Make again. Stop.` The version of the virtualbox-modules that it is trying to build is correct, though. – illabout Mar 09 '18 at 16:22