1
1
mirror of https://github.com/nmattia/niv.git synced 2024-09-16 01:47:08 +03:00

format with nixpkgs-fmt

I also couldn't help myself to change the `with { ... }` blocks
 to `let ... in` blocks.
This commit is contained in:
zimbatm 2019-11-16 15:41:14 +00:00
parent 6da5c19aa2
commit 1cb695fe7e
No known key found for this signature in database
GPG Key ID: 71BAF6D40C1D63D7
4 changed files with 188 additions and 180 deletions

View File

@ -3,57 +3,67 @@
}: }:
with rec with rec
{ files = pkgs.callPackage ./nix/files.nix {}; {
files = pkgs.callPackage ./nix/files.nix {};
sourceByRegex = name: src: regexes: sourceByRegex = name: src: regexes:
builtins.path builtins.path {
{ filter = (path: type: filter = (
path: type:
let let
relPath = pkgs.lib.removePrefix (toString src + "/") (toString path); relPath = pkgs.lib.removePrefix (toString src + "/") (toString path);
accept = pkgs.lib.any (re: builtins.match re relPath != null) regexes; accept = pkgs.lib.any (re: builtins.match re relPath != null) regexes;
in accept); in
inherit name; accept
path = src; );
}; inherit name;
niv-source = sourceByRegex "niv" ./. path = src;
[ "^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-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 = haskellPackages.niv;
niv-sdist = pkgs.haskell.lib.sdistTarball niv; niv-sdist = pkgs.haskell.lib.sdistTarball niv;
niv-cabal-upload = niv-cabal-upload =
with let
{ niv-version = niv.version; niv-version = niv.version;
}; in
pkgs.writeScript "cabal-upload" pkgs.writeScript "cabal-upload"
'' ''
#!${pkgs.stdenv.shell} #!${pkgs.stdenv.shell}
cabal upload "$@" "${niv-sdist}/niv-${niv-version}.tar.gz" cabal upload "$@" "${niv-sdist}/niv-${niv-version}.tar.gz"
''; '';
# WARNING: extremely disgusting hack below. # WARNING: extremely disgusting hack below.
# #
@ -127,76 +137,76 @@ with rec
# In order to make `Paths_niv(version)` available in `ghci`, we parse the # 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 # version from `package.yaml` and create a dummy module that we inject in the
# `ghci` command. # `ghci` command.
niv-devshell = haskellPackages.shellFor niv-devshell = haskellPackages.shellFor {
{ packages = (ps: [ ps.niv ]); packages = ps: [ ps.niv ];
shellHook = shellHook = ''
'' repl_for() {
repl_for() { haskell_version=$(cat ./package.yaml \
haskell_version=$(cat ./package.yaml \ | grep -oP 'version: \K\d+.\d+.\d+' \
| grep -oP 'version: \K\d+.\d+.\d+' \ | sed 's/\./,/g' )
| 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 "module Paths_niv where" >> $paths_niv
echo "import qualified Data.Version" >> $paths_niv echo "import qualified Data.Version" >> $paths_niv
echo "version :: Data.Version.Version" >> $paths_niv echo "version :: Data.Version.Version" >> $paths_niv
echo "version = Data.Version.Version [$haskell_version] []" >> $paths_niv echo "version = Data.Version.Version [$haskell_version] []" >> $paths_niv
niv_main="" niv_main=""
shopt -s globstar shopt -s globstar
ghci -clear-package-db -global-package-db -Wall app/$1.hs src/**/*.hs $paths_niv ghci -clear-package-db -global-package-db -Wall app/$1.hs src/**/*.hs $paths_niv
} }
repl() { repl() {
repl_for NivTest repl_for NivTest
} }
repl_niv() { repl_niv() {
repl_for 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 " ..."
'';
};
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 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; }; 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"; "niv-test && touch $out";
readme = pkgs.writeText "README.md" readme = pkgs.writeText "README.md" (
(with let
{ template = builtins.readFile ./README.tpl.md; template = builtins.readFile ./README.tpl.md;
niv_help = builtins.readFile niv_help = builtins.readFile (
(pkgs.runCommand "niv_help" { buildInputs = [ niv ]; } pkgs.runCommand "niv_help" { buildInputs = [ niv ]; }
"niv --help > $out" "niv --help > $out"
); );
niv_cmd_help = cmd: builtins.readFile niv_cmd_help = cmd: builtins.readFile (
(pkgs.runCommand "niv_${cmd}_help" { buildInputs = [ niv ]; } pkgs.runCommand "niv_${cmd}_help" { buildInputs = [ niv ]; }
"niv ${cmd} --help > $out" "niv ${cmd} --help > $out"
); );
cmds = [ "add" "update" "drop" "init" "show" ]; cmds = [ "add" "update" "drop" "init" "show" ];
}; in
pkgs.lib.replaceStrings pkgs.lib.replaceStrings
([ "replace_niv_help" ] ++ (map (cmd: "replace_niv_${cmd}_help") cmds)) ([ "replace_niv_help" ] ++ (map (cmd: "replace_niv_${cmd}_help") cmds))
([ niv_help ] ++ (map niv_cmd_help cmds)) ([ niv_help ] ++ (map niv_cmd_help cmds))
template template
); );
readme-test = pkgs.runCommand "README-test" {} readme-test = pkgs.runCommand "README-test" {}
'' ''
err() { err() {
@ -235,21 +245,20 @@ rec
[ $expected_hash == $actual_hash ] && echo dymmy > $out || err [ $expected_hash == $actual_hash ] && echo dymmy > $out || err
''; '';
# TODO: use nivForTest for this one # TODO: use nivForTest for this one
niv-svg-cmds = pkgs.writeScript "niv-svg-cmds" niv-svg-cmds = pkgs.writeScript "niv-svg-cmds"
'' ''
#!${pkgs.stdenv.shell} #!${pkgs.stdenv.shell}
set -euo pipefail set -euo pipefail
echo '$ niv init' echo '$ niv init'
niv init niv init
echo echo
echo '$ niv add stedolan/jq' echo '$ niv add stedolan/jq'
niv add stedolan/jq niv add stedolan/jq
''; '';
niv-svg-gen = pkgs.writeScript "niv-svg-gen" niv-svg-gen = pkgs.writeScript "niv-svg-gen"
'' ''
#!${pkgs.stdenv.shell} #!${pkgs.stdenv.shell}
set -euo pipefail set -euo pipefail
export PATH=${haskellPackages.niv}/bin:${pkgs.nix}/bin:$PATH export PATH=${haskellPackages.niv}/bin:${pkgs.nix}/bin:$PATH
@ -262,6 +271,5 @@ rec
echo done rendering echo done rendering
popd popd
''; '';
} }

View File

@ -1,13 +1,8 @@
{ sources ? import ./sources.nix }: { sources ? import ./sources.nix }:
import sources.nixpkgs import sources.nixpkgs {
{ overlays = overlays = [
[ (_: pkgs: (_: pkgs: { inherit sources; })
{ inherit sources; } (_: pkgs: { termtosvg = pkgs.callPackage ./termtosvg.nix {}; })
) ];
(_: pkgs: config = {};
{ termtosvg = pkgs.callPackage ./termtosvg.nix {}; } }
)
];
config = {};
}

View File

@ -6,42 +6,40 @@ with rec
pkgs = pkgs =
if hasNixpkgsPath if hasNixpkgsPath
then then
if hasThisAsNixpkgsPath if hasThisAsNixpkgsPath
then import (builtins_fetchTarball { inherit (sources_nixpkgs) url sha256; }) {} then import (builtins_fetchTarball { inherit (sources_nixpkgs) url sha256; }) {}
else import <nixpkgs> {} else import <nixpkgs> {}
else else
import (builtins_fetchTarball { inherit (sources_nixpkgs) url sha256; }) {}; import (builtins_fetchTarball { inherit (sources_nixpkgs) url sha256; }) {};
sources_nixpkgs = sources_nixpkgs =
if builtins.hasAttr "nixpkgs" sources if builtins.hasAttr "nixpkgs" sources
then sources.nixpkgs then sources.nixpkgs
else abort else abort
'' ''
Please specify either <nixpkgs> (through -I or NIX_PATH=nixpkgs=...) or Please specify either <nixpkgs> (through -I or NIX_PATH=nixpkgs=...) or
add a package called "nixpkgs" to your sources.json. add a package called "nixpkgs" to your sources.json.
''; '';
# fetchTarball version that is compatible between all the versions of Nix # fetchTarball version that is compatible between all the versions of Nix
builtins_fetchTarball = builtins_fetchTarball = { url, sha256 }@attrs:
{ url, sha256 }@attrs: let
let inherit (builtins) lessThan nixVersion fetchTarball;
inherit (builtins) lessThan nixVersion fetchTarball; in
in if lessThan nixVersion "1.12" then
if lessThan nixVersion "1.12" then fetchTarball { inherit url; }
fetchTarball { inherit url; } else
else fetchTarball attrs;
fetchTarball attrs;
# fetchurl version that is compatible between all the versions of Nix # fetchurl version that is compatible between all the versions of Nix
builtins_fetchurl = builtins_fetchurl = { url, sha256 }@attrs:
{ url, sha256 }@attrs: let
let inherit (builtins) lessThan nixVersion fetchurl;
inherit (builtins) lessThan nixVersion fetchurl; in
in if lessThan nixVersion "1.12" then
if lessThan nixVersion "1.12" then fetchurl { inherit url; }
fetchurl { inherit url; } else
else fetchurl attrs;
fetchurl attrs;
# A wrapper around pkgs.fetchzip that has inspectable arguments, # A wrapper around pkgs.fetchzip that has inspectable arguments,
# annoyingly this means we have to specify them # annoyingly this means we have to specify them
@ -57,37 +55,42 @@ with rec
sources = builtins.fromJSON (builtins.readFile ./sources.json); sources = builtins.fromJSON (builtins.readFile ./sources.json);
mapAttrs = builtins.mapAttrs or mapAttrs = builtins.mapAttrs or (
(f: set: with builtins; f: set: with builtins;
listToAttrs (map (attr: { name = attr; value = f attr set.${attr}; }) (attrNames set))); listToAttrs (map (attr: { name = attr; value = f attr set.${attr}; }) (attrNames set))
);
# borrowed from nixpkgs # borrowed from nixpkgs
functionArgs = f: f.__functionArgs or (builtins.functionArgs f); functionArgs = f: f.__functionArgs or (builtins.functionArgs f);
callFunctionWith = autoArgs: f: args: callFunctionWith = autoArgs: f: args:
let auto = builtins.intersectAttrs (functionArgs f) autoArgs; let
in f (auto // args); auto = builtins.intersectAttrs (functionArgs f) autoArgs;
in
f (auto // args);
getFetcher = spec: getFetcher = spec:
let fetcherName = let
if builtins.hasAttr "type" spec fetcherName =
then builtins.getAttr "type" spec if builtins.hasAttr "type" spec
else "builtin-tarball"; then builtins.getAttr "type" spec
in builtins.getAttr fetcherName { else "builtin-tarball";
"tarball" = fetchzip; in
"builtin-tarball" = builtins_fetchTarball; builtins.getAttr fetcherName {
"file" = fetchurl; "tarball" = fetchzip;
"builtin-url" = builtins_fetchurl; "builtin-tarball" = builtins_fetchTarball;
}; "file" = fetchurl;
"builtin-url" = builtins_fetchurl;
};
}; };
# NOTE: spec must _not_ have an "outPath" attribute # NOTE: spec must _not_ have an "outPath" attribute
mapAttrs (_: spec: mapAttrs (
if builtins.hasAttr "outPath" spec _: spec:
then abort if builtins.hasAttr "outPath" spec
"The values in sources.json should not have an 'outPath' attribute" then abort
else "The values in sources.json should not have an 'outPath' attribute"
if builtins.hasAttr "url" spec && builtins.hasAttr "sha256" spec else
then if builtins.hasAttr "url" spec && builtins.hasAttr "sha256" spec
spec // then
{ outPath = callFunctionWith spec (getFetcher spec) { }; } spec // { outPath = callFunctionWith spec (getFetcher spec) {}; }
else spec else spec
) sources ) sources

View File

@ -18,15 +18,17 @@ let
niv_HEAD = "a489b65a5c3a29983701069d1ce395b23d9bde64"; niv_HEAD = "a489b65a5c3a29983701069d1ce395b23d9bde64";
niv_HEAD- = "abc51449406ba3279c466b4d356b4ae8522ceb58"; niv_HEAD- = "abc51449406ba3279c466b4d356b4ae8522ceb58";
nixpkgs-channels_HEAD = "571b40d3f50466d3e91c1e609d372de96d782793"; nixpkgs-channels_HEAD = "571b40d3f50466d3e91c1e609d372de96d782793";
in pkgs.runCommand "test" in
{ buildInputs = pkgs.runCommand "test"
[ pkgs.haskellPackages.wai-app-static {
niv buildInputs = [
pkgs.nix pkgs.haskellPackages.wai-app-static
pkgs.jq niv
pkgs.netcat-gnu pkgs.nix
]; pkgs.jq
} pkgs.netcat-gnu
];
}
'' ''
set -euo pipefail set -euo pipefail