about summary refs log tree commit diff
path: root/modules/common/nix.nix
diff options
context:
space:
mode:
authorAzat Bahawi <azat@bahawi.net>2024-04-21 02:15:42 +0300
committerAzat Bahawi <azat@bahawi.net>2024-04-21 02:15:42 +0300
commite6ed60548397627bf10f561f9438201dbba0a36e (patch)
treef9a84c5957d2cc4fcd148065ee9365a0c851ae1c /modules/common/nix.nix
parent2024-04-18 (diff)
2024-04-21
Diffstat (limited to '')
-rw-r--r--modules/common/nix.nix150
1 files changed, 150 insertions, 0 deletions
diff --git a/modules/common/nix.nix b/modules/common/nix.nix
new file mode 100644
index 0000000..db46336
--- /dev/null
+++ b/modules/common/nix.nix
@@ -0,0 +1,150 @@
+{
+  config,
+  inputs,
+  lib,
+  pkgs,
+  this,
+  ...
+}:
+with lib;
+let
+  cfg = config.nixfiles.modules.common.nix;
+in
+{
+  options.nixfiles.modules.common.nix.allowedUnfreePackages = mkOption {
+    description = "A list of allowed unfree packages.";
+    type = with types; listOf str;
+    default = [ ];
+  };
+
+  config = {
+    _module.args =
+      let
+        importNixpkgs =
+          nixpkgs:
+          import nixpkgs {
+            inherit (config.nixpkgs) config;
+            inherit (this) system;
+          };
+      in
+      rec {
+        pkgsLocal = importNixpkgs "${config.my.home}/src/nixpkgs"; # Impure!
+        pkgsMaster = importNixpkgs inputs.nixpkgs-master;
+        pkgsStable = importNixpkgs inputs.nixpkgs-stable;
+        pkgsRev =
+          rev: hash:
+          importNixpkgs (
+            pkgs.fetchFromGitHub {
+              owner = "NixOS";
+              repo = "nixpkgs";
+              inherit rev hash;
+            }
+          );
+        pkgsPr = pr: pkgsRev "refs/pull/${toString pr}/head";
+      };
+
+    hm = {
+      # Used primarily in conjunction with the "nixfiles" script.
+      home.file.".nix-defexpr/default.nix".text =
+        let
+          hostname = strings.escapeNixIdentifier this.hostname;
+        in
+        optionalString this.isHeadful ''
+          let
+            self = builtins.getFlake "nixfiles";
+            configurations = self.nixosConfigurations;
+            local = configurations.${hostname};
+          in rec {
+            inherit self;
+            inherit (self) inputs lib;
+            inherit (lib) my;
+            this = my.configurations.${hostname};
+            inherit (local) config;
+            inherit (local.config.system.build) toplevel vm vmWithBootLoader manual;
+            pretty = expr: lib.trace (lib.generators.toPretty {} expr) {};
+          } // configurations // local._module.args
+        '';
+
+      programs.bash.shellAliases.nix = "nix --verbose --print-build-logs";
+    };
+
+    nix =
+      let
+        notSelfInputs = filterAttrs (n: _: n != "self") inputs;
+      in
+      {
+        daemonCPUSchedPolicy = "idle";
+        daemonIOSchedClass = "idle";
+        daemonIOSchedPriority = 7;
+
+        settings = {
+          # https://nixos.org/manual/nix/unstable/contributing/experimental-features.html#currently-available-experimental-features
+          # https://github.com/NixOS/nix/blob/master/src/libutil/experimental-features.cc
+          experimental-features = concatStringsSep " " [
+            "flakes"
+            "nix-command"
+            "recursive-nix"
+            "repl-flake"
+          ];
+
+          keep-derivations = if this.isHeadful then "true" else "false";
+          keep-outputs = if this.isHeadful then "true" else "false";
+
+          flake-registry = "${inputs.flake-registry}/flake-registry.json";
+
+          warn-dirty = false;
+
+          keep-going = true;
+
+          substituters = [
+            "https://azahi.cachix.org"
+            "https://nix-community.cachix.org"
+          ];
+          trusted-public-keys = [
+            "azahi.cachix.org-1:2bayb+iWYMAVw3ZdEpVg+NPOHCXncw7WMQ0ElX1GO3s="
+            "nix-community.cachix.org-1:mB9FSh9qf2dCimDSUo8Zy7bkq5CX+/rkCWyvRCYg3Fs="
+          ];
+
+          trusted-users = [
+            "root"
+            my.username
+          ];
+        };
+
+        nixPath = mapAttrsToList (n: v: "${n}=${v}") notSelfInputs ++ [
+          "nixfiles=${config.my.home}/src/nixfiles"
+        ];
+
+        registry = mapAttrs (_: flake: { inherit flake; }) notSelfInputs // {
+          nixfiles.flake = inputs.self;
+        };
+      };
+
+    nixpkgs = {
+      config.allowUnfreePredicate = p: elem (getName p) cfg.allowedUnfreePackages;
+
+      overlays = with inputs; [
+        self.overlays.default
+        (_: _: { })
+      ];
+    };
+
+    environment = {
+      localBinInPath = true;
+      defaultPackages = mkForce [ ];
+      systemPackages =
+        with pkgs;
+        optionals this.isHeadful [
+          nix-top
+          nix-tree
+          nixfiles
+        ];
+      sessionVariables = {
+        NIXFILES = "${config.my.home}/src/nixfiles";
+        NIX_SHELL_PRESERVE_PROMPT = "1";
+      };
+    };
+
+    system.stateVersion = with builtins; head (split "\n" (readFile "${inputs.nixpkgs}/.version"));
+  };
+}

Consider giving Nix/NixOS a try! <3