From 8f4afa9d00f617b6c41d8e715b93c681bdb72b31 Mon Sep 17 00:00:00 2001 From: seth Date: Tue, 6 Aug 2024 23:43:22 -0400 Subject: [PATCH] refactor(nix): cleanup package --- flake.nix | 4 +-- nix/derivation.nix | 75 ---------------------------------------------- nix/package.nix | 68 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 70 insertions(+), 77 deletions(-) delete mode 100644 nix/derivation.nix create mode 100644 nix/package.nix diff --git a/flake.nix b/flake.nix index 2d71623..d55c078 100644 --- a/flake.nix +++ b/flake.nix @@ -85,7 +85,7 @@ }; in { - refraction = pkgs.callPackage ./nix/derivation.nix { inherit self; }; + refraction = pkgs.callPackage ./nix/package.nix { }; static-x86_64 = mkStatic { arch = "x86_64"; }; static-aarch64 = mkStatic { arch = "aarch64"; }; @@ -97,7 +97,7 @@ ); overlays.default = _: prev: { - refraction = prev.callPackage ./nix/derivation.nix { inherit self; }; + refraction = prev.callPackage ./nix/package.nix { }; }; }; } diff --git a/nix/derivation.nix b/nix/derivation.nix deleted file mode 100644 index 94b81a0..0000000 --- a/nix/derivation.nix +++ /dev/null @@ -1,75 +0,0 @@ -{ - lib, - stdenv, - go, - rustPlatform, - darwin, - self, - lto ? true, - optimizeSize ? false, -}: -rustPlatform.buildRustPackage { - pname = "refraction"; - version = - (lib.importTOML ../Cargo.toml).package.version - + "-${self.shortRev or self.dirtyShortRev or "unknown-dirty"}"; - - __structuredAttrs = true; - - src = lib.fileset.toSource { - root = ../.; - fileset = lib.fileset.unions [ - ../src - ../build.rs - ../Cargo.lock - ../Cargo.toml - ../tags - ]; - }; - - cargoLock = { - lockFile = ../Cargo.lock; - }; - - buildInputs = lib.optionals stdenv.hostPlatform.isDarwin ( - with darwin.apple_sdk.frameworks; - [ - CoreFoundation - Security - SystemConfiguration - ] - ); - - env = - let - toRustFlags = lib.mapAttrs' ( - name: - lib.nameValuePair "CARGO_PROFILE_RELEASE_${ - lib.toUpper (builtins.replaceStrings [ "-" ] [ "_" ] name) - }" - ); - in - lib.optionalAttrs lto (toRustFlags { - lto = "thin"; - }) - // lib.optionalAttrs optimizeSize (toRustFlags { - codegen-units = "1"; - opt-level = "s"; - panic = "abort"; - strip = "symbols"; - }); - - # useful for container images - passthru.architecture = go.GOARCH; - - meta = with lib; { - mainProgram = "refraction"; - description = "Discord bot for Prism Launcher"; - homepage = "https://github.com/PrismLauncher/refraction"; - license = licenses.gpl3Plus; - maintainers = with maintainers; [ - getchoo - Scrumplex - ]; - }; -} diff --git a/nix/package.nix b/nix/package.nix new file mode 100644 index 0000000..4bc93e6 --- /dev/null +++ b/nix/package.nix @@ -0,0 +1,68 @@ +{ + lib, + stdenv, + go, + rustPlatform, + lto ? !optimizeSize, + optimizeSize ? false, +}: + +let + fs = lib.fileset; + toRustFlags = flags: toString (lib.mapAttrsToList (name: value: "-C ${name}=${value}") flags); +in +assert lib.assertMsg (lto -> !optimizeSize) "`lto` and `optimizeSize` are mutually exclusive"; +rustPlatform.buildRustPackage rec { + pname = "refraction"; + inherit (passthru.cargoToml.package) version; + + src = fs.toSource { + root = ../.; + fileset = fs.intersection (fs.gitTracked ../.) ( + fs.unions [ + ../src + ../build.rs + ../Cargo.lock + ../Cargo.toml + ../tags + ] + ); + }; + + cargoLock.lockFile = ../Cargo.lock; + + # `panic=abort` breaks tests womp womp + doCheck = stdenv.buildPlatform.canExecute stdenv.hostPlatform && !optimizeSize; + + env = { + RUSTFLAGS = toRustFlags ( + lib.optionalAttrs lto { + lto = "thin"; + embed-bicode = "yes"; + } + // lib.optionalAttrs optimizeSize { + codegen-units = "1"; + opt-level = "s"; + panic = "abort"; + strip = "symbols"; + } + ); + }; + + passthru = { + cargoToml = lib.importTOML ../Cargo.toml; + # For container images + dockerArchitecture = go.GOARCH; + }; + + meta = { + description = "Discord bot for Prism Launcher"; + homepage = "https://github.com/PrismLauncher/refraction"; + license = lib.licenses.gpl3Plus; + maintainers = with lib.maintainers; [ + getchoo + Scrumplex + ]; + mainProgram = "refraction"; + }; +}