Skip to content

Commit

Permalink
Add NixOS modules with settings for the NVIDIA driver
Browse files Browse the repository at this point in the history
The simplest way to use the modules is to import `nixosModules.default`
from the flake (or `modules.default` if using NUR); the `default` module
imports all modules that don't cause any immediate config changes when
imported.  However, all modules are also available as separate entries
under `nixosModules` (or `modules` for NUR), but using those separate
modules requires knowing the dependencies between them (some modules set
config options which are defined in other modules from here).

New module options:

- `sigprof.hardware.gpu.driver.nvidia.enable` — enable several options
  required to use the proprietary NVIDIA driver (should be suitable for
  the latest version of the driver);

- `sigprof.hardware.gpu.driver.nvidia.legacy_340.enable` — enable even
  more options required to use the legacy version 340 of the proprietary
  NVIDIA driver (also enables the above option to get generic NVIDIA
  driver settings);

- `sigprof.nixpkgs.permittedUnfreePackages` — allowed unfree package
  names as a string list (unlike `nixpkgs.config.allowUnfreePredicate`,
  this option is mergeable, therefore multiple features that require
  unfree packages can append their package names to the list).  Sets
  `nixpkgs.config.allowUnfreePredicate` internally, therefore that
  option could not be used anymore.
  • Loading branch information
sigprof committed Jul 17, 2022
1 parent 34fcf10 commit bb4957c
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 1 deletion.
2 changes: 1 addition & 1 deletion default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@
in
self.nurPackages.${pkgs.system}
// {
inherit (self) lib;
inherit (self) lib modules;
}
1 change: 1 addition & 0 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
}:
{
lib = import ./lib inputs;
nixosModules = import ./modules {inherit self inputs;};
overlays.default = import ./overlay.nix;
}
// (
Expand Down
12 changes: 12 additions & 0 deletions modules/default.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{self, ...}: let
inherit (self.lib.modules) exportFlakeLocalModules;
in
exportFlakeLocalModules self {
dir = ./.;
modules = [
hardware/gpu/driver/nvidia
hardware/gpu/driver/nvidia/legacy_340.nix
nixpkgs/permitted-unfree-packages.nix
];
mergedModuleName = "default";
}
18 changes: 18 additions & 0 deletions modules/hardware/gpu/driver/nvidia/default.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
lib,
config,
...
}: {
options = {
sigprof.hardware.gpu.driver.nvidia.enable =
lib.mkEnableOption "the proprietary NVIDIA driver";
};

config = lib.mkIf config.sigprof.hardware.gpu.driver.nvidia.enable {
services.xserver.videoDrivers = ["nvidia"];
sigprof.nixpkgs.permittedUnfreePackages = [
"nvidia-x11"
"nvidia-settings"
];
};
}
23 changes: 23 additions & 0 deletions modules/hardware/gpu/driver/nvidia/legacy_340.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
lib,
config,
pkgs,
...
}: {
options = {
sigprof.hardware.gpu.driver.nvidia.legacy_340.enable =
lib.mkEnableOption "the legacy version 340 of the proprietary NVIDIA driver";
};

config = lib.mkIf config.sigprof.hardware.gpu.driver.nvidia.legacy_340.enable {
sigprof.hardware.gpu.driver.nvidia.enable = true;

hardware.nvidia.package = config.boot.kernelPackages.nvidiaPackages.legacy_340;

# The legacy 340 driver does not compile against kernels newer than 5.4.x.
boot.kernelPackages = pkgs.linuxPackages_5_4;

# Workaround for https://github.com/NixOS/nixpkgs/issues/120602
hardware.opengl.setLdLibraryPath = true;
};
}
25 changes: 25 additions & 0 deletions modules/nixpkgs/permitted-unfree-packages.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
config,
lib,
...
}: let
inherit (lib) literalExpression mkIf mkOption types;
cfg = config.sigprof.nixpkgs;
in {
options = {
sigprof.nixpkgs.permittedUnfreePackages = mkOption {
type = types.listOf types.str;
default = [];
example = literalExpression "[\"hplip\"]";
description = ''
List of package names that would be permitted to use despite having an
unfree license.
'';
};
};

config = mkIf (cfg.permittedUnfreePackages != []) {
nixpkgs.config.allowUnfreePredicate = pkg:
builtins.elem (lib.getName pkg) cfg.permittedUnfreePackages;
};
}
4 changes: 4 additions & 0 deletions nur.nix
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
};

lib = import ./lib self.inputs;
modules = import ./modules {
inherit self;
inherit (self) inputs;
};
nurPackages.${system} =
(pkgs.callPackage ./pkgs {inherit (self) inputs;})
.packages;
Expand Down

0 comments on commit bb4957c

Please sign in to comment.