From f275964bb0ffae1ddaddd53ec19dfceef9cc90ba Mon Sep 17 00:00:00 2001 From: Victor Engmark Date: Sun, 12 Mar 2023 11:41:42 +0000 Subject: [PATCH] refactor: statix recommendations (#363) * refactor: Inherit where possible As recommended by `statix check`. * refactor: Remove unnecessary parentheses As recommended by `statix check`. * refactor: Remove unnecessary comparison with `true` As recommended by `statix check`. * refactor: Avoid using deprecated `isNull` As recommended by `statix check` and the documentation . * refactor: Simplify conditionals As recommended by `statix check`. * Update sources version --------- Co-authored-by: Nicolas Mattia --- default.nix | 16 +++++++--------- foo/default.nix | 2 +- nix/sources.nix | 21 +++++++++++---------- src/Niv/Sources.hs | 3 ++- 4 files changed, 21 insertions(+), 21 deletions(-) diff --git a/default.nix b/default.nix index 73765e9..d0384ef 100644 --- a/default.nix +++ b/default.nix @@ -7,14 +7,12 @@ let sourceByRegex = name: src: regexes: builtins.path { - filter = ( - path: type: - let - relPath = pkgs.lib.removePrefix (toString src + "/") (toString path); - accept = pkgs.lib.any (re: builtins.match re relPath != null) regexes; - in - accept - ); + filter = path: type: + let + relPath = pkgs.lib.removePrefix (toString src + "/") (toString path); + accept = pkgs.lib.any (re: builtins.match re relPath != null) regexes; + in + accept; inherit name; path = src; }; @@ -61,7 +59,7 @@ let }; }; - niv = haskellPackages.niv; + inherit (haskellPackages) niv; niv-sdist = pkgs.haskell.lib.sdistTarball niv; diff --git a/foo/default.nix b/foo/default.nix index a51218f..ab78fcf 100644 --- a/foo/default.nix +++ b/foo/default.nix @@ -65,7 +65,7 @@ { mkDerivation }: mkDerivation { pname = spec.name; - version = spec.version; + inherit (spec) version; inherit src; isLibrary = builtins.hasAttr "library" spec; isExecutable = builtins.hasAttr "executables" spec; diff --git a/nix/sources.nix b/nix/sources.nix index 23ab29b..fe3dadf 100644 --- a/nix/sources.nix +++ b/nix/sources.nix @@ -27,16 +27,17 @@ let fetch_git = name: spec: let ref = - if spec ? ref then spec.ref else - if spec ? branch then "refs/heads/${spec.branch}" else - if spec ? tag then "refs/tags/${spec.tag}" else - abort "In git source '${name}': Please specify `ref`, `tag` or `branch`!"; - submodules = if spec ? submodules then spec.submodules else false; + spec.ref or ( + if spec ? branch then "refs/heads/${spec.branch}" else + if spec ? tag then "refs/tags/${spec.tag}" else + abort "In git source '${name}': Please specify `ref`, `tag` or `branch`!" + ); + submodules = spec.submodules or false; submoduleArg = let nixSupportsSubmodules = builtins.compareVersions builtins.nixVersion "2.4" >= 0; emptyArgWithWarning = - if submodules == true + if submodules then builtins.trace ( @@ -115,7 +116,7 @@ let # the path directly as opposed to the fetched source. replace = name: drv: let - saneName = stringAsChars (c: if isNull (builtins.match "[a-zA-Z0-9]" c) then "_" else c) name; + saneName = stringAsChars (c: if (builtins.match "[a-zA-Z0-9]" c) == null then "_" else c) name; ersatz = builtins.getEnv "NIV_OVERRIDE_${saneName}"; in if ersatz == "" then drv else @@ -151,7 +152,7 @@ let inherit (builtins) lessThan nixVersion fetchTarball; in if lessThan nixVersion "1.12" then - fetchTarball ({ inherit url; } // (optionalAttrs (!isNull name) { inherit name; })) + fetchTarball ({ inherit url; } // (optionalAttrs (name != null) { inherit name; })) else fetchTarball attrs; @@ -161,7 +162,7 @@ let inherit (builtins) lessThan nixVersion fetchurl; in if lessThan nixVersion "1.12" then - fetchurl ({ inherit url; } // (optionalAttrs (!isNull name) { inherit name; })) + fetchurl ({ inherit url; } // (optionalAttrs (name != null) { inherit name; })) else fetchurl attrs; @@ -182,7 +183,7 @@ let # The "config" used by the fetchers mkConfig = { sourcesFile ? if builtins.pathExists ./sources.json then ./sources.json else null - , sources ? if isNull sourcesFile then { } else builtins.fromJSON (builtins.readFile sourcesFile) + , sources ? if sourcesFile == null then { } else builtins.fromJSON (builtins.readFile sourcesFile) , system ? builtins.currentSystem , pkgs ? mkPkgs sources system }: rec { diff --git a/src/Niv/Sources.hs b/src/Niv/Sources.hs index ff8b75f..751c455 100644 --- a/src/Niv/Sources.hs +++ b/src/Niv/Sources.hs @@ -179,6 +179,7 @@ data SourcesNixVersion | -- Support submodules for git repos V27 | -- formatting fix + -- Apply statix suggestions V28 deriving stock (Bounded, Enum, Eq) @@ -252,7 +253,7 @@ sourcesVersionToMD5 = \case V25 -> "6612caee5814670e5e4d9dd1b71b5f70" V26 -> "937bff93370a064c9000f13cec5867f9" V27 -> "8031ba9d8fbbc7401c800d0b84278ec8" - V28 -> "2ed0d91d652b28d99f14659144a0af28" + V28 -> "26ed55356db7673935329210a4f8c4a5" -- | The MD5 sum of ./nix/sources.nix sourcesNixMD5 :: IO T.Text