NixOS on the Framework Laptop 16

Not sure if this has been posted anywhere yet, but I hacked up a quick way to add the kernel patch from [ANNOUNCEMENT] Adaptive Sync / Freesync / VRR not working to get VRR working under NixOS without waiting for kernel 6.9.

The first file just creates a derivation of the amdgpu kernel module. I saved this as amdgpu-kernel-module.nix in the same folder as my system config file.

{ pkgs, lib, kernel ? pkgs.linuxPackages_latest.kernel }:

pkgs.stdenv.mkDerivation {
  pname = "amdgpu-kernel-module";
  inherit (kernel) src version postPatch nativeBuildInputs;

  kernel_dev = kernel.dev;
  kernelVersion = kernel.modDirVersion;

  modulePath = "drivers/gpu/drm/amd/amdgpu";

  buildPhase = ''
    BUILT_KERNEL=$kernel_dev/lib/modules/$kernelVersion/build

    cp $BUILT_KERNEL/Module.symvers .
    cp $BUILT_KERNEL/.config        .
    cp $kernel_dev/vmlinux          .

    make "-j$NIX_BUILD_CORES" modules_prepare
    make "-j$NIX_BUILD_CORES" M=$modulePath modules
  '';

  installPhase = ''
    make \
      INSTALL_MOD_PATH="$out" \
      XZ="xz -T$NIX_BUILD_CORES" \
      M="$modulePath" \
      modules_install
  '';

  meta = {
    description = "AMD GPU kernel module";
    license = lib.licenses.gpl3;
  };
}

Then in you system config file, you need to call the package, add it as an extra-kernel module and apply the patch, like so:

{ pkgs, config, ... }:
let
  amdgpu-kernel-module = pkgs.callPackage ./amdgpu-kernel-module.nix {
    kernel = config.boot.kernelPackages.kernel;
  };
in
{
# ...your normal config.nix file

boot.extraModulePackages = [
    (amdgpu-kernel-module.overrideAttrs (_: {
      patches = [
        # vrr fix
        (pkgs.fetchurl {
          url = "https://gitlab.freedesktop.org/agd5f/linux/-/commit/2f14c0c8cae8e9e3b603a3f91909baba66540027.diff";
          hash = "sha256-0++twr9t4AkJXZfj0aHGMVDuOhxtLP/q2d4FGfggnww=";
        })
      ];
    }))
  ];
}

This patches a single in-tree kernel module and then loads it as if it were an out-of-tree kernel module. This also means, you won’t need to recompile the whole kernel, this just recompiles the amdgpu module, which takes about 1m30s on my Framework 16.
After applying this patch you can just enable VRR in your favorite compositor or desktop like normal.

I tested this with the linuxPackages_latest package, not sure if it will work with older kernels.

5 Likes