Optimize for each system individually #168

Closed
DarkKirb wants to merge 10 commits from per-system-optim into main
14 changed files with 260 additions and 35 deletions

View file

@ -1,6 +1,8 @@
{
config,
pkgs,
lib,
system,
...
}: {
imports = [

View file

@ -14,4 +14,9 @@
boot.kernelPackages = lib.mkForce pkgs.linuxPackages_testing_bcachefs;
networking.hostId = "8425e349";
nix.settings.post-build-hook = lib.mkForce "true";
nixpkgs.localSystem = {
system = "x86_64-linux";
gcc.arch = "skylake";
gcc.tune = "skylake";
};
}

View file

@ -145,4 +145,9 @@
};
services.restic.backups.sysbackup.paths = ["/persist"];
nixpkgs.localSystem = {
system = "aarch64-linux";
gcc.arch = "armv8.2-a";
gcc.tune = "neoverse-n1";
};
}

View file

@ -170,4 +170,10 @@
"/ip4/0.0.0.0/tcp/5001"
"/ip6/::/tcp/5001"
]; # Only exposed over the tailed scale
nixpkgs.localSystem = {
system = "x86_64-linux";
gcc.arch = "znver1";
gcc.tune = "znver1";
};
}

View file

@ -14,4 +14,9 @@
boot.kernelPackages = lib.mkForce pkgs.linuxPackages_testing_bcachefs;
networking.hostId = "8425e349";
nix.settings.post-build-hook = lib.mkForce "true";
nixpkgs.localSystem = {
system = "x86_64-linux";
gcc.arch = "skylake";
gcc.tune = "skylake";
};
}

View file

@ -206,4 +206,9 @@
services.resolved.enable = false;
services.bind.forwarders = lib.mkForce [];
services.tailscale.useRoutingFeatures = "server";
nixpkgs.localSystem = {
system = "x86_64-linux";
gcc.arch = "skylake";
gcc.tune = "skylake";
};
}

View file

@ -121,4 +121,9 @@
];
services.tailscale.useRoutingFeatures = "client";
home-manager.users.darkkirb._module.args.withNSFW = lib.mkForce true;
nixpkgs.localSystem = {
system = "x86_64-linux";
gcc.arch = "znver2";
gcc.tune = "znver2";
};
}

View file

@ -1,19 +1,12 @@
# Configuration file configuring specialization
{
pkgs,
config,
lib,
...
}: {
nixpkgs.overlays = [
(self: prev: {
custom_xanmod = pkgs.linuxPackagesFor (pkgs.linuxKernel.kernels.linux_xanmod.override {
ignoreConfigErrors = true;
autoModules = true;
kernelPreferBuiltin = true;
enableParallelBuilding = true;
extraConfig = import (../extra/linux/config- + "${config.networking.hostName}.nix");
});
})
];
{config, ...}: {
environment.etc."nix/local-system.json".text = builtins.toJSON {
inherit (config.nixpkgs.localSystem) system gcc rustc linux-kernel;
};
# Instead of overriding stdenv, we provide a user hook that appends the correct configure flags
nixpkgs.config.stdenv.userHook = ''
NIX_CFLAGS_COMPILE+="-O3 -pipe -flto"
unset doCheck doInstallCheck
'';
}

View file

@ -180,4 +180,9 @@
hardware.bluetooth.enable = true;
services.blueman.enable = true;
services.tailscale.useRoutingFeatures = "client";
nixpkgs.localSystem = {
system = "x86_64-linux";
gcc.arch = "skylake";
gcc.tune = "skylake";
};
}

136
extra/nixpkgs.patch Normal file
View file

@ -0,0 +1,136 @@
diff --git a/lib/systems/architectures.nix b/lib/systems/architectures.nix
index 94127fa9..5253e4bb 100644
--- a/lib/systems/architectures.nix
+++ b/lib/systems/architectures.nix
@@ -32,9 +32,24 @@ rec {
armv5te = [ ];
armv6 = [ ];
armv7-a = [ ];
- armv8-a = [ ];
+ armv8-a = [ "fp" "simd" ];
+ "armv8.1-a" = [ "crc" "lse" "rdma" ] ++ features.armv8-a;
+ "armv8.2-a" = features."armv8.1-a";
+ "armv8.3-a" = [ "pauth" ] ++ features."armv8.2-a";
+ "armv8.4-a" = [ "flagm" "fp16fml" "dotprod" ] ++ features."armv8.3-a";
+ "armv8.5-a" = [ "sb" "sbss" "predres" ] ++ features."armv8.4-a";
+ "armv8.6-a" = [ "bf16" "i8mm" ] ++ features."armv8.5-a";
+ "armv8.7-a" = [ "ls64" ] ++ features."armv8.6-a";
+ "armv8.8-a" = [ "mops" ] ++ features."armv8.7-a";
+ "armv9-a" = [ "sve" "sve2" ] ++ features."armv8.5-a";
+ "armv9.1-a" = [ "bf16" "i8mm" ] ++ features."armv9-a";
+ "armv9.2-a" = [ "ls64" ] ++ features."armv9.1-a";
+ "armv9.3-a" = [ "mops" ] ++ features."armv9.2-a";
mips32 = [ ];
loongson2f = [ ];
+ riscv64-g = [ "multiply" "atomic" "float" "double" "csr" "ifencei" ];
+ riscv64-gc = [ "compressed" ] ++ features.riscv64-g;
+ riscv64-gcv = [ "vector" ] ++ features.riscv64-gc;
};
# a superior CPU has all the features of an inferior and is able to build and test code for it
@@ -85,8 +100,23 @@ rec {
armv6 = [ ];
armv7-a = [ ];
armv8-a = [ ];
+ "armv8.1-a" = [ "armv8-a" ] ++ inferiors.armv8-a;
+ "armv8.2-a" = [ "armv8.1-a" ] ++ inferiors."armv8.1-a";
+ "armv8.3-a" = [ "armv8.2-a" ] ++ inferiors."armv8.2-a";
+ "armv8.4-a" = [ "armv8.3-a" ] ++ inferiors."armv8.3-a";
+ "armv8.5-a" = [ "armv8.4-a" ] ++ inferiors."armv8.4-a";
+ "armv8.6-a" = [ "armv8.5-a" ] ++ inferiors."armv8.5-a";
+ "armv8.7-a" = [ "armv8.6-a" ] ++ inferiors."armv8.6-a";
+ "armv8.8-a" = [ "armv8.7-a" ] ++ inferiors."armv8.7-a";
+ "armv9-a" = [ "armv8.5-a" ] ++ inferiors."armv8.5-a";
+ "armv9.1-a" = [ "armv9-a" "armv8.6-a" ] ++ inferiors."armv9-a" ++ inferiors."armv8.6-a";
+ "armv9.2-a" = [ "armv9.1-a" "armv8.7-a" ] ++ inferiors."armv9.1-a" ++ inferiors."armv8.7-a";
+ "armv9.3-a" = [ "armv9.2-a" "armv8.8-a" ] ++ inferiors."armv9.2-a" ++ inferiors."armv8.8-a";
mips32 = [ ];
loongson2f = [ ];
+ riscv64-g = [ ];
+ riscv64-gc = [ "riscv64-g" ] ++ inferiors.riscv64-g;
+ riscv64-gcv = [ "riscv64-gc" ] ++ inferiors.riscv64-gc;
};
predicates = let
diff --git a/pkgs/build-support/cc-wrapper/default.nix b/pkgs/build-support/cc-wrapper/default.nix
index e0ee3dae41f..49dbbe56594 100644
--- a/pkgs/build-support/cc-wrapper/default.nix
+++ b/pkgs/build-support/cc-wrapper/default.nix
@@ -122,6 +122,10 @@ let
znver1 = versionAtLeast ccVersion "6.0";
znver2 = versionAtLeast ccVersion "9.0";
znver3 = versionAtLeast ccVersion "11.0";
+ # ARM
+ # TODO: Fill in as needed
+ # RISCV
+ # TODO: Fill in as needed
}.${arch} or true
else if isClang then
{ # Intel
@@ -132,6 +136,10 @@ let
# AMD
znver1 = versionAtLeast ccVersion "4.0";
znver2 = versionAtLeast ccVersion "9.0";
+ # AMD
+ # TODO: Fill in as needed
+ # RISCV
+ # TODO: Fill in as needed
}.${arch} or true
else
false;
diff --git a/pkgs/stdenv/cross/default.nix b/pkgs/stdenv/cross/default.nix
index bf410ec0..10b240b7 100644
--- a/pkgs/stdenv/cross/default.nix
+++ b/pkgs/stdenv/cross/default.nix
@@ -26,9 +26,9 @@ in lib.init bootStages ++ [
inherit config overlays;
selfBuild = false;
stdenv =
- assert vanillaPackages.stdenv.buildPlatform == localSystem;
- assert vanillaPackages.stdenv.hostPlatform == localSystem;
- assert vanillaPackages.stdenv.targetPlatform == localSystem;
+ assert vanillaPackages.stdenv.buildPlatform.system == localSystem.system;
+ assert vanillaPackages.stdenv.hostPlatform.system == localSystem.system;
+ assert vanillaPackages.stdenv.targetPlatform.system == localSystem.system;
vanillaPackages.stdenv.override { targetPlatform = crossSystem; };
# It's OK to change the built-time dependencies
allowCustomOverrides = true;
diff --git a/pkgs/top-level/impure.nix b/pkgs/top-level/impure.nix
index 4d847e280f4..874b776c1e1 100644
--- a/pkgs/top-level/impure.nix
+++ b/pkgs/top-level/impure.nix
@@ -9,12 +9,15 @@ let
# Return x if it evaluates, or def if it throws an exception.
try = x: def: let res = builtins.tryEval x; in if res.success then res.value else def;
+ configuredLocalSystem = if builtins.pathExists "/etc/nix/local-system.json" then builtins.fromJSON (builtins.readFile "/etc/nix/local-system.json") else { system = builtins.currentSystem; };
+ localSystemOverriden = args: args ? system && (args.system != configuredLocalSystem);
+ defaultLocalSystem = args: if localSystemOverriden args then { system = args.system; } else configuredLocalSystem;
in
{ # We put legacy `system` into `localSystem`, if `localSystem` was not passed.
# If neither is passed, assume we are building packages on the current
# (build, in GNU Autotools parlance) platform.
- localSystem ? { system = args.system or builtins.currentSystem; }
+ localSystem ? null
# These are needed only because nix's `--arg` command-line logic doesn't work
# with unnamed parameters allowed by ...
@@ -78,6 +81,9 @@ in
, ...
} @ args:
+let
+ localSystem' = if localSystem == null then defaultLocalSystem args else localSystem;
+in
# If `localSystem` was explicitly passed, legacy `system` should
# not be passed, and vice-versa.
@@ -85,5 +91,6 @@ assert args ? localSystem -> !(args ? system);
assert args ? system -> !(args ? localSystem);
import ./. (builtins.removeAttrs args [ "system" ] // {
- inherit config overlays localSystem;
+ inherit config overlays;
+ localSystem = localSystem';
})

View file

@ -107,6 +107,22 @@ rec {
nur,
...
} @ args: let
nixpkgsFor = system:
import ./utils/patched-nixpkgs.nix {
inherit nixpkgs system;
patches = [./extra/nixpkgs.patch];
};
pkgsFor = system:
import (nixpkgsFor system) {
localSystem.system = system;
overlays = [
self.overlays.${system}
nur.overlay
args.prismmc.overlay
];
config.allowUnfree = true;
config.contentAddressedByDefault = true;
};
systems = [
{
name = "nixos-8gb-fsn1-1"; # Hetzner Server
@ -143,16 +159,21 @@ rec {
name,
system,
configName ? name,
}: {
}: let
nixpkgs' = nixpkgsFor system;
pkgs = pkgsFor system;
nixosSystem = import "${nixpkgs'}/nixos/lib/eval-config.nix";
in {
inherit name;
value =
nixpkgs.lib.nixosSystem
nixosSystem
{
inherit system;
specialArgs =
args
// {
inherit system;
nixpkgs = nixpkgs';
};
modules = [
(./config + "/${configName}.nix")
@ -163,9 +184,13 @@ rec {
nixpkgs.overlays = [
nur.overlay
];
home-manager.extraSpecialArgs = args // {inherit system;};
home-manager.extraSpecialArgs = args // {inherit system; nixpkgs = nixpkgs';};
})
(import utils/link-input.nix args)
(import utils/link-input.nix (args // {nixpkgs = nixpkgs';}))
{
system.nixos.versionSuffix = ".${pkgs.lib.substring 0 8 (self.lastModifiedDate or self.lastModified or "19700101")}.${self.shortRev or "dirty"}";
system.nixos.revision = pkgs.lib.mkIf (nixpkgs ? rev) nixpkgs.rev;
}
];
};
})
@ -175,10 +200,7 @@ rec {
aarch64-linux = import ./overlays args "aarch64-linux";
};
devShell.x86_64-linux = let
pkgs = import nixpkgs {
system = "x86_64-linux";
overlays = [self.overlays.x86_64-linux];
};
pkgs = pkgsFor "x86_64-linux";
in
pkgs.mkShell {
nativeBuildInputs = with pkgs; [
@ -197,10 +219,11 @@ rec {
};
formatter.x86_64-linux = nixpkgs.legacyPackages.x86_64-linux.alejandra;
packages.x86_64-linux = let
pkgs = import nixpkgs {
pkgs = import (nixpkgsFor "x86_64-linux") {
system = "x86_64-linux";
overlays = [self.overlays.x86_64-linux];
config.allowUnfree = true;
config.contentAddressedByDefault = true;
};
in {
neovim-base = args.nix-neovim.buildNeovim {
@ -213,10 +236,11 @@ rec {
};
};
packages.aarch64-linux = let
pkgs = import nixpkgs {
pkgs = import (nixpkgsFor "aarch64-linux") {
system = "aarch64-linux";
overlays = [self.overlays.aarch64-linux];
config.allowUnfree = true;
config.contentAddressedByDefault = true;
};
in {
neovim-base = args.nix-neovim.buildNeovim {

View file

@ -1,7 +1,20 @@
inputs: {lib, ...}: let
inputs': {
system,
lib,
...
}: let
# Taken from https://github.com/gytis-ivaskevicius/flake-utils-plus/blob/master/lib/options.nix
inherit (lib) filterAttrs mapAttrs';
flakes = filterAttrs (name: value: (name != "self") && (value ? outputs)) inputs;
inputs =
inputs'
// {
nixpkgs = import ./patched-nixpkgs.nix {
inherit (inputs') nixpkgs;
inherit system;
patches = "${inputs'.self}/extra/nixpkgs.patch";
};
};
flakes = filterAttrs (name: value: value ? outputs) inputs;
nixRegistry =
builtins.mapAttrs
(name: v: {flake = v;})
@ -9,11 +22,16 @@ inputs: {lib, ...}: let
in {
nix.registry = nixRegistry;
environment.etc =
mapAttrs'
(name: value: {
name = "nix/inputs/${name}";
value = {source = value.outPath;};
})
inputs;
(
mapAttrs'
(name: value: {
name = "nix/inputs/${name}";
value = {source = value.outPath;};
})
inputs
)
// {
"nix/inputs/nixpkgs-overlays".source = lib.mkForce ./nixpkgs-overlays;
};
nix.nixPath = ["/etc/nix/inputs"];
}

View file

@ -0,0 +1 @@
(import <self>).overlays.${builtins.currentSystem}

15
utils/patched-nixpkgs.nix Normal file
View file

@ -0,0 +1,15 @@
{
nixpkgs,
patches,
system,
}: let
pkgs = import nixpkgs {
inherit system;
};
nixpkgs-new = pkgs.applyPatches {
name = "nixpkgs-patched";
src = nixpkgs;
inherit patches;
};
in
nixpkgs-new