From c14012774a0d8e50932edfbaf34dd1238b23b458 Mon Sep 17 00:00:00 2001
From: Julien Moutinho <julm@sourcephile.fr>
Date: Sat, 13 Feb 2021 07:13:54 +0100
Subject: [PATCH] nix: add machine.CPUs

---
 machines/losurdo.nix               | 1 +
 machines/losurdo/unbound.nix       | 4 ++--
 machines/mermet.nix                | 1 +
 machines/mermet/dovecot.nix        | 4 ++--
 machines/mermet/openldap.nix       | 4 ++--
 machines/mermet/unbound.nix        | 4 ++--
 nixos/profiles/hardware/apu2e4.nix | 4 ++--
 nixos/profiles/hardware/dl10j.nix  | 4 ++--
 nixos/profiles/services/nginx.nix  | 4 ++--
 9 files changed, 16 insertions(+), 14 deletions(-)

diff --git a/machines/losurdo.nix b/machines/losurdo.nix
index 5929976..fbc0757 100644
--- a/machines/losurdo.nix
+++ b/machines/losurdo.nix
@@ -6,6 +6,7 @@
 system = "x86_64-linux";
 #config.allowUnfree = true;
 extraArgs = {
+  CPUs = 2;
   wireguard = rec {
     wg-intra = {
       ipv4 = "192.168.42.2";
diff --git a/machines/losurdo/unbound.nix b/machines/losurdo/unbound.nix
index ec59242..4f6a90a 100644
--- a/machines/losurdo/unbound.nix
+++ b/machines/losurdo/unbound.nix
@@ -1,4 +1,4 @@
-{ pkgs, lib, config, ... }:
+{ pkgs, lib, config, machine, ... }:
 let
   inherit (config.services) unbound;
   inherit (config.users) users;
@@ -76,7 +76,7 @@ services.unbound = {
 
       # Number of threads to create. 1 disables threading.
       # This should equal the number of CPU cores in the machine.
-      num-threads: ${toString config.nix.maxJobs}
+      num-threads: ${toString machine.CPUs}
 
       # The number of slabs to use for cache and must be a power of 2 times the
       # number of num-threads set above. more slabs reduce lock contention,
diff --git a/machines/mermet.nix b/machines/mermet.nix
index e6e1af8..28c0e71 100644
--- a/machines/mermet.nix
+++ b/machines/mermet.nix
@@ -5,6 +5,7 @@
 {
 system = "x86_64-linux";
 extraArgs = rec {
+  CPUs = 4;
   ipv4 = "80.67.180.129";
   wireguard = rec {
     wg-intra = {
diff --git a/machines/mermet/dovecot.nix b/machines/mermet/dovecot.nix
index 8a4582a..14eb019 100644
--- a/machines/mermet/dovecot.nix
+++ b/machines/mermet/dovecot.nix
@@ -1,4 +1,4 @@
-{ inputs, pkgs, lib, config, system, ... }:
+{ inputs, pkgs, lib, config, system, machine, ... }:
 let
   inherit (builtins) toString toFile readFile;
   inherit (lib) types;
@@ -284,7 +284,7 @@ services.dovecot2 = {
       postmaster_address = postmaster+dovecot+lmtp@${networking.domain}
     }
     service lmtp {
-      process_min_avail = ${toString config.nix.maxJobs}
+      process_min_avail = ${toString machine.CPUs}
       unix_listener /var/lib/postfix/queue/private/dovecot-lmtp {
         user  = ${postfix.user}
         group = ${postfix.group}
diff --git a/machines/mermet/openldap.nix b/machines/mermet/openldap.nix
index a7eb467..ad22a19 100644
--- a/machines/mermet/openldap.nix
+++ b/machines/mermet/openldap.nix
@@ -1,4 +1,4 @@
-{ pkgs, lib, config, ... }:
+{ pkgs, lib, config, machine, ... }:
 let
   inherit (builtins) baseNameOf readFile;
   inherit (lib) types;
@@ -46,7 +46,7 @@ services.openldap = {
       olcLogLevel = [ "stats" ];
       # The tool-threads parameter sets the actual amount of CPU's
       # that is used for indexing.
-      olcToolThreads = toString config.nix.maxJobs;
+      olcToolThreads = toString machine.CPUs;
     };
     children = {
       "cn=schema".includes = [
diff --git a/machines/mermet/unbound.nix b/machines/mermet/unbound.nix
index ec59242..4f6a90a 100644
--- a/machines/mermet/unbound.nix
+++ b/machines/mermet/unbound.nix
@@ -1,4 +1,4 @@
-{ pkgs, lib, config, ... }:
+{ pkgs, lib, config, machine, ... }:
 let
   inherit (config.services) unbound;
   inherit (config.users) users;
@@ -76,7 +76,7 @@ services.unbound = {
 
       # Number of threads to create. 1 disables threading.
       # This should equal the number of CPU cores in the machine.
-      num-threads: ${toString config.nix.maxJobs}
+      num-threads: ${toString machine.CPUs}
 
       # The number of slabs to use for cache and must be a power of 2 times the
       # number of num-threads set above. more slabs reduce lock contention,
diff --git a/nixos/profiles/hardware/apu2e4.nix b/nixos/profiles/hardware/apu2e4.nix
index cfa6e8f..ea38262 100644
--- a/nixos/profiles/hardware/apu2e4.nix
+++ b/nixos/profiles/hardware/apu2e4.nix
@@ -1,10 +1,10 @@
-{ pkgs, lib, config, ... }:
+{ pkgs, lib, config, machine, ... }:
 {
 hardware.cpu.amd.updateMicrocode = true;
 nix = {
   # Too CPU hungry for the APU2, for too little Mio saved
   autoOptimiseStore = false;
-  maxJobs = 4;
+  maxJobs = machine.CPUs / 2;
 };
 powerManagement.cpuFreqGovernor = lib.mkDefault "ondemand";
 
diff --git a/nixos/profiles/hardware/dl10j.nix b/nixos/profiles/hardware/dl10j.nix
index 8d67ce8..cd3b4a3 100644
--- a/nixos/profiles/hardware/dl10j.nix
+++ b/nixos/profiles/hardware/dl10j.nix
@@ -1,10 +1,10 @@
-{ pkgs, lib, config, ... }:
+{ pkgs, lib, config, machine, ... }:
 {
 hardware.cpu.intel.updateMicrocode = true;
 nix = {
   # Too CPU hungry for the DL10J, for too little Mio saved
   autoOptimiseStore = false;
-  maxJobs = 2;
+  maxJobs = machine.CPUs;
 };
 powerManagement.cpuFreqGovernor = lib.mkDefault "ondemand";
 
diff --git a/nixos/profiles/services/nginx.nix b/nixos/profiles/services/nginx.nix
index 9cbfedb..f6ad30a 100644
--- a/nixos/profiles/services/nginx.nix
+++ b/nixos/profiles/services/nginx.nix
@@ -1,4 +1,4 @@
-{ inputs, pkgs, lib, config, ... }:
+{ inputs, pkgs, lib, config, machine, ... }:
 let
   inherit (lib) types;
   inherit (config) networking;
@@ -202,7 +202,7 @@ services.nginx = {
       '';
     });
   appendConfig = ''
-    worker_processes ${toString config.nix.maxJobs};
+    worker_processes ${toString machine.CPUs};
   '';
 };
 };
-- 
2.47.2