From 1cb695fe7e5d3cf455d91718303d14e87225d40b Mon Sep 17 00:00:00 2001 From: zimbatm Date: Sat, 16 Nov 2019 15:41:14 +0000 Subject: [PATCH] format with nixpkgs-fmt I also couldn't help myself to change the `with { ... }` blocks to `let ... in` blocks. --- default.nix | 226 ++++++++++++++++++++++++---------------------- nix/default.nix | 19 ++-- nix/sources.nix | 103 +++++++++++---------- tests/default.nix | 20 ++-- 4 files changed, 188 insertions(+), 180 deletions(-) diff --git a/default.nix b/default.nix index f394b14..a46aeb1 100644 --- a/default.nix +++ b/default.nix @@ -3,57 +3,67 @@ }: with rec -{ files = pkgs.callPackage ./nix/files.nix {}; +{ + files = pkgs.callPackage ./nix/files.nix {}; + sourceByRegex = name: src: regexes: - builtins.path - { filter = (path: type: + 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); - inherit name; - path = src; - }; - niv-source = sourceByRegex "niv" ./. - [ "^package.yaml$" - "^README.md$" - "^LICENSE$" - "^app$" - "^app.*.hs$" - "^src$" - "^src/Niv$" - "^src/Niv/GitHub$" - "^src/Niv/Update$" - "^src.*.hs$" - "^README.md$" - "^nix$" - "^nix.sources.nix$" - ]; - - haskellPackages = pkgs.haskellPackages.override - { overrides = _: haskellPackages: - { niv = - pkgs.haskell.lib.failOnAllWarnings ( - pkgs.haskell.lib.disableExecutableProfiling ( - pkgs.haskell.lib.disableLibraryProfiling ( - pkgs.haskell.lib.generateOptparseApplicativeCompletion "niv" ( - haskellPackages.callCabal2nix "niv" niv-source {})))); - }; + in + accept + ); + inherit name; + path = src; }; + niv-source = sourceByRegex "niv" ./. [ + "^package.yaml$" + "^README.md$" + "^LICENSE$" + "^app$" + "^app.*.hs$" + "^src$" + "^src/Niv$" + "^src/Niv/GitHub$" + "^src/Niv/Update$" + "^src.*.hs$" + "^README.md$" + "^nix$" + "^nix.sources.nix$" + ]; + + haskellPackages = pkgs.haskellPackages.override { + overrides = _: haskellPackages: { + niv = + pkgs.haskell.lib.failOnAllWarnings ( + pkgs.haskell.lib.disableExecutableProfiling ( + pkgs.haskell.lib.disableLibraryProfiling ( + pkgs.haskell.lib.generateOptparseApplicativeCompletion "niv" ( + haskellPackages.callCabal2nix "niv" niv-source {} + ) + ) + ) + ); + }; + }; + niv = haskellPackages.niv; niv-sdist = pkgs.haskell.lib.sdistTarball niv; niv-cabal-upload = - with - { niv-version = niv.version; - }; - pkgs.writeScript "cabal-upload" - '' - #!${pkgs.stdenv.shell} - cabal upload "$@" "${niv-sdist}/niv-${niv-version}.tar.gz" - ''; + let + niv-version = niv.version; + in + pkgs.writeScript "cabal-upload" + '' + #!${pkgs.stdenv.shell} + cabal upload "$@" "${niv-sdist}/niv-${niv-version}.tar.gz" + ''; # WARNING: extremely disgusting hack below. # @@ -127,76 +137,76 @@ with rec # In order to make `Paths_niv(version)` available in `ghci`, we parse the # version from `package.yaml` and create a dummy module that we inject in the # `ghci` command. - niv-devshell = haskellPackages.shellFor - { packages = (ps: [ ps.niv ]); - shellHook = - '' - repl_for() { - haskell_version=$(cat ./package.yaml \ - | grep -oP 'version: \K\d+.\d+.\d+' \ - | sed 's/\./,/g' ) + niv-devshell = haskellPackages.shellFor { + packages = ps: [ ps.niv ]; + shellHook = '' + repl_for() { + haskell_version=$(cat ./package.yaml \ + | grep -oP 'version: \K\d+.\d+.\d+' \ + | sed 's/\./,/g' ) - paths_niv=$(mktemp -d)/Paths_niv.hs + paths_niv=$(mktemp -d)/Paths_niv.hs - echo "module Paths_niv where" >> $paths_niv - echo "import qualified Data.Version" >> $paths_niv - echo "version :: Data.Version.Version" >> $paths_niv - echo "version = Data.Version.Version [$haskell_version] []" >> $paths_niv + echo "module Paths_niv where" >> $paths_niv + echo "import qualified Data.Version" >> $paths_niv + echo "version :: Data.Version.Version" >> $paths_niv + echo "version = Data.Version.Version [$haskell_version] []" >> $paths_niv - niv_main="" + niv_main="" - shopt -s globstar - ghci -clear-package-db -global-package-db -Wall app/$1.hs src/**/*.hs $paths_niv - } + shopt -s globstar + ghci -clear-package-db -global-package-db -Wall app/$1.hs src/**/*.hs $paths_niv + } - repl() { - repl_for NivTest - } + repl() { + repl_for NivTest + } - repl_niv() { - repl_for Niv - } - - echo "To start a REPL for the test suite, run:" - echo " > repl" - echo " > :main" - echo " (tests run)" - echo - echo "To start a REPL session emulating the niv executable, run:" - echo " > repl_niv" - echo " > :main --help ..." - echo " NIV - Version manager for Nix projects" - echo " ..." - ''; - }; + repl_niv() { + repl_for Niv + } + echo "To start a REPL for the test suite, run:" + echo " > repl" + echo " > :main" + echo " (tests run)" + echo + echo "To start a REPL session emulating the niv executable, run:" + echo " > repl_niv" + echo " > :main --help ..." + echo " NIV - Version manager for Nix projects" + echo " ..." + ''; + }; }; rec -{ inherit niv niv-sdist niv-source niv-devshell niv-cabal-upload; +{ + inherit niv niv-sdist niv-source niv-devshell niv-cabal-upload; tests = pkgs.callPackage ./tests { inherit niv; }; - niv-test = pkgs.runCommand "niv-test" { buildInputs = [ niv ] ; } + niv-test = pkgs.runCommand "niv-test" { buildInputs = [ niv ]; } "niv-test && touch $out"; - readme = pkgs.writeText "README.md" - (with - { template = builtins.readFile ./README.tpl.md; - niv_help = builtins.readFile - (pkgs.runCommand "niv_help" { buildInputs = [ niv ]; } - "niv --help > $out" - ); - niv_cmd_help = cmd: builtins.readFile - (pkgs.runCommand "niv_${cmd}_help" { buildInputs = [ niv ]; } - "niv ${cmd} --help > $out" - ); - cmds = [ "add" "update" "drop" "init" "show" ]; - }; - pkgs.lib.replaceStrings - ([ "replace_niv_help" ] ++ (map (cmd: "replace_niv_${cmd}_help") cmds)) - ([ niv_help ] ++ (map niv_cmd_help cmds)) - template - ); + readme = pkgs.writeText "README.md" ( + let + template = builtins.readFile ./README.tpl.md; + niv_help = builtins.readFile ( + pkgs.runCommand "niv_help" { buildInputs = [ niv ]; } + "niv --help > $out" + ); + niv_cmd_help = cmd: builtins.readFile ( + pkgs.runCommand "niv_${cmd}_help" { buildInputs = [ niv ]; } + "niv ${cmd} --help > $out" + ); + cmds = [ "add" "update" "drop" "init" "show" ]; + in + pkgs.lib.replaceStrings + ([ "replace_niv_help" ] ++ (map (cmd: "replace_niv_${cmd}_help") cmds)) + ([ niv_help ] ++ (map niv_cmd_help cmds)) + template + ); + readme-test = pkgs.runCommand "README-test" {} '' err() { @@ -235,21 +245,20 @@ rec [ $expected_hash == $actual_hash ] && echo dymmy > $out || err ''; - # TODO: use nivForTest for this one niv-svg-cmds = pkgs.writeScript "niv-svg-cmds" - '' - #!${pkgs.stdenv.shell} - set -euo pipefail - echo '$ niv init' - niv init - echo - echo '$ niv add stedolan/jq' - niv add stedolan/jq - ''; + '' + #!${pkgs.stdenv.shell} + set -euo pipefail + echo '$ niv init' + niv init + echo + echo '$ niv add stedolan/jq' + niv add stedolan/jq + ''; niv-svg-gen = pkgs.writeScript "niv-svg-gen" - '' + '' #!${pkgs.stdenv.shell} set -euo pipefail export PATH=${haskellPackages.niv}/bin:${pkgs.nix}/bin:$PATH @@ -262,6 +271,5 @@ rec echo done rendering popd - ''; - + ''; } diff --git a/nix/default.nix b/nix/default.nix index 61724e5..144718e 100644 --- a/nix/default.nix +++ b/nix/default.nix @@ -1,13 +1,8 @@ { sources ? import ./sources.nix }: -import sources.nixpkgs - { overlays = - [ (_: pkgs: - { inherit sources; } - ) - (_: pkgs: - { termtosvg = pkgs.callPackage ./termtosvg.nix {}; } - ) - - ]; - config = {}; - } +import sources.nixpkgs { + overlays = [ + (_: pkgs: { inherit sources; }) + (_: pkgs: { termtosvg = pkgs.callPackage ./termtosvg.nix {}; }) + ]; + config = {}; +} diff --git a/nix/sources.nix b/nix/sources.nix index d4ac577..2de89a6 100644 --- a/nix/sources.nix +++ b/nix/sources.nix @@ -6,42 +6,40 @@ with rec pkgs = if hasNixpkgsPath then - if hasThisAsNixpkgsPath - then import (builtins_fetchTarball { inherit (sources_nixpkgs) url sha256; }) {} - else import {} + if hasThisAsNixpkgsPath + then import (builtins_fetchTarball { inherit (sources_nixpkgs) url sha256; }) {} + else import {} else - import (builtins_fetchTarball { inherit (sources_nixpkgs) url sha256; }) {}; + import (builtins_fetchTarball { inherit (sources_nixpkgs) url sha256; }) {}; sources_nixpkgs = if builtins.hasAttr "nixpkgs" sources then sources.nixpkgs else abort - '' + '' Please specify either (through -I or NIX_PATH=nixpkgs=...) or add a package called "nixpkgs" to your sources.json. - ''; + ''; # fetchTarball version that is compatible between all the versions of Nix - builtins_fetchTarball = - { url, sha256 }@attrs: - let - inherit (builtins) lessThan nixVersion fetchTarball; - in - if lessThan nixVersion "1.12" then - fetchTarball { inherit url; } - else - fetchTarball attrs; + builtins_fetchTarball = { url, sha256 }@attrs: + let + inherit (builtins) lessThan nixVersion fetchTarball; + in + if lessThan nixVersion "1.12" then + fetchTarball { inherit url; } + else + fetchTarball attrs; # fetchurl version that is compatible between all the versions of Nix - builtins_fetchurl = - { url, sha256 }@attrs: - let - inherit (builtins) lessThan nixVersion fetchurl; - in - if lessThan nixVersion "1.12" then - fetchurl { inherit url; } - else - fetchurl attrs; + builtins_fetchurl = { url, sha256 }@attrs: + let + inherit (builtins) lessThan nixVersion fetchurl; + in + if lessThan nixVersion "1.12" then + fetchurl { inherit url; } + else + fetchurl attrs; # A wrapper around pkgs.fetchzip that has inspectable arguments, # annoyingly this means we have to specify them @@ -57,37 +55,42 @@ with rec sources = builtins.fromJSON (builtins.readFile ./sources.json); - mapAttrs = builtins.mapAttrs or - (f: set: with builtins; - listToAttrs (map (attr: { name = attr; value = f attr set.${attr}; }) (attrNames set))); + mapAttrs = builtins.mapAttrs or ( + f: set: with builtins; + listToAttrs (map (attr: { name = attr; value = f attr set.${attr}; }) (attrNames set)) + ); # borrowed from nixpkgs functionArgs = f: f.__functionArgs or (builtins.functionArgs f); callFunctionWith = autoArgs: f: args: - let auto = builtins.intersectAttrs (functionArgs f) autoArgs; - in f (auto // args); + let + auto = builtins.intersectAttrs (functionArgs f) autoArgs; + in + f (auto // args); getFetcher = spec: - let fetcherName = - if builtins.hasAttr "type" spec - then builtins.getAttr "type" spec - else "builtin-tarball"; - in builtins.getAttr fetcherName { - "tarball" = fetchzip; - "builtin-tarball" = builtins_fetchTarball; - "file" = fetchurl; - "builtin-url" = builtins_fetchurl; - }; + let + fetcherName = + if builtins.hasAttr "type" spec + then builtins.getAttr "type" spec + else "builtin-tarball"; + in + builtins.getAttr fetcherName { + "tarball" = fetchzip; + "builtin-tarball" = builtins_fetchTarball; + "file" = fetchurl; + "builtin-url" = builtins_fetchurl; + }; }; # NOTE: spec must _not_ have an "outPath" attribute -mapAttrs (_: spec: - if builtins.hasAttr "outPath" spec - then abort - "The values in sources.json should not have an 'outPath' attribute" - else - if builtins.hasAttr "url" spec && builtins.hasAttr "sha256" spec - then - spec // - { outPath = callFunctionWith spec (getFetcher spec) { }; } - else spec - ) sources +mapAttrs ( + _: spec: + if builtins.hasAttr "outPath" spec + then abort + "The values in sources.json should not have an 'outPath' attribute" + else + if builtins.hasAttr "url" spec && builtins.hasAttr "sha256" spec + then + spec // { outPath = callFunctionWith spec (getFetcher spec) {}; } + else spec +) sources diff --git a/tests/default.nix b/tests/default.nix index ba31e81..18a958e 100644 --- a/tests/default.nix +++ b/tests/default.nix @@ -18,15 +18,17 @@ let niv_HEAD = "a489b65a5c3a29983701069d1ce395b23d9bde64"; niv_HEAD- = "abc51449406ba3279c466b4d356b4ae8522ceb58"; nixpkgs-channels_HEAD = "571b40d3f50466d3e91c1e609d372de96d782793"; -in pkgs.runCommand "test" - { buildInputs = - [ pkgs.haskellPackages.wai-app-static - niv - pkgs.nix - pkgs.jq - pkgs.netcat-gnu - ]; - } +in +pkgs.runCommand "test" + { + buildInputs = [ + pkgs.haskellPackages.wai-app-static + niv + pkgs.nix + pkgs.jq + pkgs.netcat-gnu + ]; + } '' set -euo pipefail