From 3ecb2fc790b3a26a7839fd8e665e29c4b8599264 Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Mon, 24 Mar 2025 19:54:09 -0400 Subject: [PATCH] Enable optimized builds for steam deck. --- nix/steam_deck/configuration/home.nix | 1 + .../configuration/hosts/deck/default.nix | 1 + .../roles/2ship2harkinian/default.nix | 52 +++++++++---- .../roles/global_options/default.nix | 19 +++++ .../configuration/roles/pcsx2/default.nix | 52 +++++++++---- .../configuration/roles/rpcs3/default.nix | 52 +++++++++---- .../roles/shipwright/default.nix | 54 +++++++++---- .../configuration/roles/sm64ex/default.nix | 78 ++++++++++++------- 8 files changed, 211 insertions(+), 98 deletions(-) create mode 100644 nix/steam_deck/configuration/roles/global_options/default.nix diff --git a/nix/steam_deck/configuration/home.nix b/nix/steam_deck/configuration/home.nix index 03d5dc8..fc6454f 100644 --- a/nix/steam_deck/configuration/home.nix +++ b/nix/steam_deck/configuration/home.nix @@ -8,6 +8,7 @@ { imports = [ ./roles/2ship2harkinian + ./roles/global_options ./roles/graphics ./roles/pcsx2 ./roles/rpcs3 diff --git a/nix/steam_deck/configuration/hosts/deck/default.nix b/nix/steam_deck/configuration/hosts/deck/default.nix index 63966d0..bd204ba 100644 --- a/nix/steam_deck/configuration/hosts/deck/default.nix +++ b/nix/steam_deck/configuration/hosts/deck/default.nix @@ -10,6 +10,7 @@ config = { me.graphical = true; + me.optimizations.enable = true; me.pcsx2.enable = true; me.rpcs3.enable = true; me.ship2harkinian.enable = true; diff --git a/nix/steam_deck/configuration/roles/2ship2harkinian/default.nix b/nix/steam_deck/configuration/roles/2ship2harkinian/default.nix index 2447746..0edca35 100644 --- a/nix/steam_deck/configuration/roles/2ship2harkinian/default.nix +++ b/nix/steam_deck/configuration/roles/2ship2harkinian/default.nix @@ -54,23 +54,41 @@ in }; nixpkgs.overlays = [ - (final: prev: { - _2ship2harkinian = pkgs.buildEnv { - name = prev._2ship2harkinian.name; - paths = [ - (config.lib.nixGL.wrap prev._2ship2harkinian) - ]; - extraOutputsToInstall = [ - "man" - "doc" - "info" - ]; - # We have to use 555 instead of the normal 444 here because the .desktop file ends up inside $HOME on steam deck and desktop files must be either not in $HOME or must be executable, otherwise KDE Plasma refuses to execute them. - postBuild = '' - chmod 0555 $out/share/applications/2s2h.desktop - ''; - }; - }) + ( + final: prev: + let + optimizeWithFlags = + pkg: flags: + pkg.overrideAttrs (old: { + NIX_CFLAGS_COMPILE = [ (old.NIX_CFLAGS_COMPILE or "") ] ++ flags; + }); + original_package = + if config.me.optimizations.enable then + (optimizeWithFlags prev._2ship2harkinian [ + "-march=znver2" + "-mtune=znver2" + ]) + else + prev._2ship2harkinian; + in + { + _2ship2harkinian = pkgs.buildEnv { + name = prev._2ship2harkinian.name; + paths = [ + (config.lib.nixGL.wrap original_package) + ]; + extraOutputsToInstall = [ + "man" + "doc" + "info" + ]; + # We have to use 555 instead of the normal 444 here because the .desktop file ends up inside $HOME on steam deck and desktop files must be either not in $HOME or must be executable, otherwise KDE Plasma refuses to execute them. + postBuild = '' + chmod 0555 $out/share/applications/2s2h.desktop + ''; + }; + } + ) ]; }) ] diff --git a/nix/steam_deck/configuration/roles/global_options/default.nix b/nix/steam_deck/configuration/roles/global_options/default.nix new file mode 100644 index 0000000..4f917d1 --- /dev/null +++ b/nix/steam_deck/configuration/roles/global_options/default.nix @@ -0,0 +1,19 @@ +{ + config, + lib, + pkgs, + ... +}: + +{ + imports = [ ]; + + options.me = { + optimizations.enable = lib.mkOption { + type = lib.types.bool; + default = false; + example = true; + description = "Whether we want to enable CPU optimizations (will trigger a rebuild from source)."; + }; + }; +} diff --git a/nix/steam_deck/configuration/roles/pcsx2/default.nix b/nix/steam_deck/configuration/roles/pcsx2/default.nix index 616bffd..bfd7551 100644 --- a/nix/steam_deck/configuration/roles/pcsx2/default.nix +++ b/nix/steam_deck/configuration/roles/pcsx2/default.nix @@ -70,23 +70,41 @@ in }; nixpkgs.overlays = [ - (final: prev: { - pcsx2 = pkgs.buildEnv { - name = prev.pcsx2.name; - paths = [ - (config.lib.nixGL.wrap prev.pcsx2) - ]; - extraOutputsToInstall = [ - "man" - "doc" - "info" - ]; - # We have to use 555 instead of the normal 444 here because the .desktop file ends up inside $HOME on steam deck and desktop files must be either not in $HOME or must be executable, otherwise KDE Plasma refuses to execute them. - postBuild = '' - chmod 0555 $out/share/applications/PCSX2.desktop - ''; - }; - }) + ( + final: prev: + let + optimizeWithFlags = + pkg: flags: + pkg.overrideAttrs (old: { + NIX_CFLAGS_COMPILE = [ (old.NIX_CFLAGS_COMPILE or "") ] ++ flags; + }); + original_package = + if config.me.optimizations.enable then + (optimizeWithFlags prev.pcsx2 [ + "-march=znver2" + "-mtune=znver2" + ]) + else + prev.pcsx2; + in + { + pcsx2 = pkgs.buildEnv { + name = prev.pcsx2.name; + paths = [ + (config.lib.nixGL.wrap original_package) + ]; + extraOutputsToInstall = [ + "man" + "doc" + "info" + ]; + # We have to use 555 instead of the normal 444 here because the .desktop file ends up inside $HOME on steam deck and desktop files must be either not in $HOME or must be executable, otherwise KDE Plasma refuses to execute them. + postBuild = '' + chmod 0555 $out/share/applications/PCSX2.desktop + ''; + }; + } + ) ]; }) ] diff --git a/nix/steam_deck/configuration/roles/rpcs3/default.nix b/nix/steam_deck/configuration/roles/rpcs3/default.nix index 6d5a6bf..819846e 100644 --- a/nix/steam_deck/configuration/roles/rpcs3/default.nix +++ b/nix/steam_deck/configuration/roles/rpcs3/default.nix @@ -70,23 +70,41 @@ in }; nixpkgs.overlays = [ - (final: prev: { - rpcs3 = pkgs.buildEnv { - name = prev.rpcs3.name; - paths = [ - (config.lib.nixGL.wrap prev.rpcs3) - ]; - extraOutputsToInstall = [ - "man" - "doc" - "info" - ]; - # We have to use 555 instead of the normal 444 here because the .desktop file ends up inside $HOME on steam deck and desktop files must be either not in $HOME or must be executable, otherwise KDE Plasma refuses to execute them. - # postBuild = '' - # chmod 0555 $out/share/applications/PCSX2.desktop - # ''; - }; - }) + ( + final: prev: + let + optimizeWithFlags = + pkg: flags: + pkg.overrideAttrs (old: { + NIX_CFLAGS_COMPILE = [ (old.NIX_CFLAGS_COMPILE or "") ] ++ flags; + }); + original_package = + if config.me.optimizations.enable then + (optimizeWithFlags prev.rpcs3 [ + "-march=znver2" + "-mtune=znver2" + ]) + else + prev.rpcs3; + in + { + rpcs3 = pkgs.buildEnv { + name = prev.rpcs3.name; + paths = [ + (config.lib.nixGL.wrap original_package) + ]; + extraOutputsToInstall = [ + "man" + "doc" + "info" + ]; + # We have to use 555 instead of the normal 444 here because the .desktop file ends up inside $HOME on steam deck and desktop files must be either not in $HOME or must be executable, otherwise KDE Plasma refuses to execute them. + postBuild = '' + chmod 0555 $out/share/applications/rpcs3.desktop + ''; + }; + } + ) ]; }) ] diff --git a/nix/steam_deck/configuration/roles/shipwright/default.nix b/nix/steam_deck/configuration/roles/shipwright/default.nix index 49ad9ee..2533ca7 100644 --- a/nix/steam_deck/configuration/roles/shipwright/default.nix +++ b/nix/steam_deck/configuration/roles/shipwright/default.nix @@ -54,23 +54,43 @@ in }; nixpkgs.overlays = [ - (final: prev: { - shipwright = pkgs.buildEnv { - name = prev.shipwright.name; - paths = [ - (config.lib.nixGL.wrap prev.shipwright) - ]; - extraOutputsToInstall = [ - "man" - "doc" - "info" - ]; - # We have to use 555 instead of the normal 444 here because the .desktop file ends up inside $HOME on steam deck and desktop files must be either not in $HOME or must be executable, otherwise KDE Plasma refuses to execute them. - postBuild = '' - chmod 0555 $out/share/applications/soh.desktop - ''; - }; - }) + ( + final: prev: + let + optimizeWithFlags = + pkg: flags: + pkg.overrideAttrs (old: { + NIX_CFLAGS_COMPILE = [ (old.NIX_CFLAGS_COMPILE or "") ] ++ flags; + }); + original_package = prev.shipwright; + # Optimization is broken for shipwright, fails to build "The following attributes are overlapping" + # original_package = + # if !config.me.optimizations.enable then + # (optimizeWithFlags prev.shipwright [ + # "-march=znver2" + # "-mtune=znver2" + # ]) + # else + # prev.shipwright; + in + { + shipwright = pkgs.buildEnv { + name = prev.shipwright.name; + paths = [ + (config.lib.nixGL.wrap original_package) + ]; + extraOutputsToInstall = [ + "man" + "doc" + "info" + ]; + # We have to use 555 instead of the normal 444 here because the .desktop file ends up inside $HOME on steam deck and desktop files must be either not in $HOME or must be executable, otherwise KDE Plasma refuses to execute them. + postBuild = '' + chmod 0555 $out/share/applications/soh.desktop + ''; + }; + } + ) ]; }) ] diff --git a/nix/steam_deck/configuration/roles/sm64ex/default.nix b/nix/steam_deck/configuration/roles/sm64ex/default.nix index a081885..6dcb4b6 100644 --- a/nix/steam_deck/configuration/roles/sm64ex/default.nix +++ b/nix/steam_deck/configuration/roles/sm64ex/default.nix @@ -52,38 +52,56 @@ in }; nixpkgs.overlays = [ - (final: prev: { - sm64ex = - let - desktop_item = pkgs.makeDesktopItem { - name = "sm64ex"; - desktopName = "Super Mario 64"; - comment = "A PC Port of Super Mario 64."; - categories = [ - "Game" + ( + final: prev: + let + optimizeWithFlags = + pkg: flags: + pkg.overrideAttrs (old: { + NIX_CFLAGS_COMPILE = [ (old.NIX_CFLAGS_COMPILE or "") ] ++ flags; + }); + original_package = + if config.me.optimizations.enable then + (optimizeWithFlags prev.sm64ex [ + "-march=znver2" + "-mtune=znver2" + ]) + else + prev.sm64ex; + in + { + sm64ex = + let + desktop_item = pkgs.makeDesktopItem { + name = "sm64ex"; + desktopName = "Super Mario 64"; + comment = "A PC Port of Super Mario 64."; + categories = [ + "Game" + ]; + icon = "sm64ex"; + type = "Application"; + exec = "sm64ex"; + }; + in + pkgs.buildEnv { + name = prev.sm64ex.name; + paths = [ + (config.lib.nixGL.wrap original_package) ]; - icon = "sm64ex"; - type = "Application"; - exec = "sm64ex"; + extraOutputsToInstall = [ + "man" + "doc" + "info" + ]; + # We have to use 555 instead of the normal 444 here because the .desktop file ends up inside $HOME on steam deck and desktop files must be either not in $HOME or must be executable, otherwise KDE Plasma refuses to execute them. + postBuild = '' + install -m 555 -D "${desktop_item}/share/applications/"* -t $out/share/applications/ + install -m 444 -D "${./files/icon.png}" $out/share/pixmaps/sm64ex.png + ''; }; - in - pkgs.buildEnv { - name = prev.sm64ex.name; - paths = [ - (config.lib.nixGL.wrap prev.sm64ex) - ]; - extraOutputsToInstall = [ - "man" - "doc" - "info" - ]; - # We have to use 555 instead of the normal 444 here because the .desktop file ends up inside $HOME on steam deck and desktop files must be either not in $HOME or must be executable, otherwise KDE Plasma refuses to execute them. - postBuild = '' - install -m 555 -D "${desktop_item}/share/applications/"* -t $out/share/applications/ - install -m 444 -D "${./files/icon.png}" $out/share/pixmaps/sm64ex.png - ''; - }; - }) + } + ) ]; }) ]