nix: refactor with flakes (#1827)

* nix: refactor with flakes

* Update github actions

* Make pre-commit-hooks/flake-utils follows flake-utils

* Update github actions

* Update github actions

* Support multi GHC versions

* Update github actions

* Copy postInstall from nixpkgs

* Remove invalid ghc865

* Update README

* Use nixpkgs-unstable

* Update README

* Add plugins automatically
This commit is contained in:
Potato Hatsue 2021-05-19 15:32:23 +08:00 committed by GitHub
parent 2d909af930
commit 9abf566e06
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 362 additions and 375 deletions

View File

@ -24,7 +24,6 @@ jobs:
strategy:
fail-fast: false
matrix:
ghc: ['default']
os: [ubuntu-latest, macOS-latest]
steps:
@ -33,8 +32,12 @@ jobs:
with:
submodules: true
- if: ${{ needs.pre_job.outputs.should_skip != 'true' }}
uses: cachix/install-nix-action@v12
uses: cachix/install-nix-action@v13
with:
install_url: https://nixos-nix-install-tests.cachix.org/serve/lb41az54kzk6j12p81br4bczary7m145/install
install_options: '--tarball-url-prefix https://nixos-nix-install-tests.cachix.org/serve'
extra_nix_config: |
experimental-features = nix-command flakes
nix_path: nixpkgs=channel:nixos-unstable
- if: ${{ needs.pre_job.outputs.should_skip != 'true' }}
uses: cachix/cachix-action@v8
@ -42,4 +45,8 @@ jobs:
name: haskell-language-server
authToken: ${{ secrets.HLS_CACHIX_AUTH_TOKEN }}
- if: ${{ needs.pre_job.outputs.should_skip != 'true' }}
run: nix-shell --argstr compiler ${{ matrix.ghc }} --run "cabal update && cabal build"
run: nix build
- if: ${{ needs.pre_job.outputs.should_skip != 'true' }}
run: nix develop --profile dev && cachix push haskell-language-server dev
- if: ${{ needs.pre_job.outputs.should_skip != 'true' }}
run: nix path-info --json | jq -r '.[].path' | cachix push haskell-language-server

4
.gitignore vendored
View File

@ -41,3 +41,7 @@ test/testdata/**/hie.yaml
# ghcide-bench
*.identifierPosition
/bench/example
# nix
result
result-doc

View File

@ -78,6 +78,7 @@ background](https://neilmitchell.blogspot.com/2020/01/one-haskell-ide-to-rule-th
- [Using Cabal](#using-cabal)
- [Using Stack](#using-stack)
- [Using Nix](#using-nix)
- [Flakes support](#flakes-support)
- [Introduction tutorial](#introduction-tutorial)
- [Test your hacked HLS in your editor](#test-your-hacked-hls-in-your-editor)
- [Adding support for a new editor](#adding-support-for-a-new-editor)
@ -807,8 +808,27 @@ $ cabal update
$ cabal build
```
##### Flakes support
If you are using nix 2.4 style command (enabled by `experimental-features = nix-command`),
you can use `nix develop` instead of `nix-shell` to enter the development shell. To enter the shell with specific GHC versions:
* `nix develop` or `nix develop .#haskell-language-server-dev` - default GHC version
* `nix develop .#haskell-language-server-8104-dev` - GHC 8.10.4
* `nix develop .#haskell-language-server-884-dev` - GHC 8.8.4
* `nix develop .#haskell-language-server-901-dev` - GHC 9.0.1
If you are looking for a Nix expression to create haskell-language-server binaries, see https://github.com/haskell/haskell-language-server/issues/122
To create binaries:
* `nix build` or `nix build .#haskell-language-server` - default GHC version
* `nix build .#haskell-language-server-8104` - GHC 8.10.4
* `nix build .#haskell-language-server-884` - GHC 8.8.4
* `nix build .#haskell-language-server-901` - GHC 9.0.1
GHC 8.6.5 is not supported here because `nixpkgs-unstable` no longer maintains the corresponding packages set.
#### Introduction tutorial
Pepeiborra [wrote an tutorial](https://github.com/pepeiborra/hls-tutorial) on writing a plugin in HLS.

11
default.nix Normal file
View File

@ -0,0 +1,11 @@
# This file is the compt layer of flakes: https://github.com/edolstra/flake-compat
# See flake.nix for details
(import (
let
lock = builtins.fromJSON (builtins.readFile ./flake.lock);
in fetchTarball {
url = "https://github.com/edolstra/flake-compat/archive/${lock.nodes.flake-compat.locked.rev}.tar.gz";
sha256 = lock.nodes.flake-compat.locked.narHash; }
) {
src = ./.;
}).defaultNix

101
flake.lock Normal file
View File

@ -0,0 +1,101 @@
{
"nodes": {
"flake-compat": {
"flake": false,
"locked": {
"lastModified": 1606424373,
"narHash": "sha256-oq8d4//CJOrVj+EcOaSXvMebvuTkmBJuT5tzlfewUnQ=",
"owner": "edolstra",
"repo": "flake-compat",
"rev": "99f1c2157fba4bfe6211a321fd0ee43199025dbf",
"type": "github"
},
"original": {
"owner": "edolstra",
"repo": "flake-compat",
"type": "github"
}
},
"flake-utils": {
"locked": {
"lastModified": 1620759905,
"narHash": "sha256-WiyWawrgmyN0EdmiHyG2V+fqReiVi8bM9cRdMaKQOFg=",
"owner": "numtide",
"repo": "flake-utils",
"rev": "b543720b25df6ffdfcf9227afafc5b8c1fabfae8",
"type": "github"
},
"original": {
"owner": "numtide",
"repo": "flake-utils",
"type": "github"
}
},
"gitignore": {
"flake": false,
"locked": {
"lastModified": 1611672876,
"narHash": "sha256-qHu3uZ/o9jBHiA3MEKHJ06k7w4heOhA+4HCSIvflRxo=",
"owner": "hercules-ci",
"repo": "gitignore.nix",
"rev": "211907489e9f198594c0eb0ca9256a1949c9d412",
"type": "github"
},
"original": {
"owner": "hercules-ci",
"repo": "gitignore.nix",
"type": "github"
}
},
"nixpkgs": {
"locked": {
"lastModified": 1621151593,
"narHash": "sha256-wC8kXOdXwI3YyAeqv6LKZCmjqnjXovHI1UzOrma4kWA=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "f2c015d33d1b36070c64b726662db42c6c87b7be",
"type": "github"
},
"original": {
"owner": "NixOS",
"ref": "nixpkgs-unstable",
"repo": "nixpkgs",
"type": "github"
}
},
"pre-commit-hooks": {
"inputs": {
"flake-utils": [
"flake-utils"
],
"nixpkgs": [
"nixpkgs"
]
},
"locked": {
"lastModified": 1620897287,
"narHash": "sha256-UU0SysjA5h1NMCQhA0lKaeqy9gCBqpq/5dW1BWLbIXo=",
"owner": "cachix",
"repo": "pre-commit-hooks.nix",
"rev": "40a51af82c1181b9dea3526c4124eee077e30213",
"type": "github"
},
"original": {
"owner": "cachix",
"repo": "pre-commit-hooks.nix",
"type": "github"
}
},
"root": {
"inputs": {
"flake-compat": "flake-compat",
"flake-utils": "flake-utils",
"gitignore": "gitignore",
"nixpkgs": "nixpkgs",
"pre-commit-hooks": "pre-commit-hooks"
}
}
},
"root": "root",
"version": 7
}

216
flake.nix Normal file
View File

@ -0,0 +1,216 @@
# Maintaining this file:
#
# - Bump the inputs version using `nix flake update`
# - Edit `sourceDirs` to update the set of local packages
#
# For more details: https://nixos.wiki/wiki/Flakes
{
description = "haskell language server flake";
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
flake-compat = {
url = "github:edolstra/flake-compat";
flake = false;
};
flake-utils.url = "github:numtide/flake-utils";
pre-commit-hooks = {
url = "github:cachix/pre-commit-hooks.nix";
inputs.nixpkgs.follows = "nixpkgs";
inputs.flake-utils.follows = "flake-utils";
};
gitignore = {
url = "github:hercules-ci/gitignore.nix";
flake = false;
};
};
outputs =
{ self, nixpkgs, flake-compat, flake-utils, pre-commit-hooks, gitignore }:
{
overlay = final: prev:
with prev;
let
haskellOverrides = {
overrides = hself: hsuper: {
# we override mkDerivation here to apply the following
# tweak to each haskell package:
# if the package is broken, then we disable its check and relax the cabal bounds;
# otherwise, we leave it unchanged.
# hopefully, this could fix packages marked as broken by nix due to check failures
# or the build failure because of tight cabal bounds
mkDerivation = args:
let
broken = args.broken or false;
check = args.doCheck or true;
jailbreak = args.jailbreak or false;
in hsuper.mkDerivation (args // {
jailbreak = if broken then true else jailbreak;
doCheck = if broken then false else check;
});
};
};
gitignoreSource = (import gitignore { inherit lib; }).gitignoreSource;
# List all subdirectories under `./plugins`, except `./plugins/default`
pluginsDir = ./plugins;
pluginSourceDirs = builtins.removeAttrs (lib.mapAttrs'
(name: _: lib.nameValuePair name (pluginsDir + ("/" + name)))
(builtins.readDir pluginsDir)) [ "default" ];
# Source directories of our packages, should be consistent with cabal.project
sourceDirs = {
haskell-language-server = ./.;
ghcide = ./ghcide;
hls-graph = ./hls-graph;
shake-bench = ./shake-bench;
hie-compat = ./hie-compat;
hls-plugin-api = ./hls-plugin-api;
hls-test-utils = ./hls-test-utils;
} // pluginSourceDirs;
# Tweak our packages
tweaks = hself: hsuper:
with haskell.lib; {
hls-hlint-plugin =
hsuper.hls-hlint-plugin.override { hlint = hself.hlint_3_2_7; };
hls-tactics-plugin = hsuper.hls-tactics-plugin.override {
refinery = hself.refinery_0_3_0_0;
};
};
hlsSources =
builtins.mapAttrs (_: dir: gitignoreSource dir) sourceDirs;
extended = hpkgs:
(hpkgs.override haskellOverrides).extend (hself: hsuper:
# disable all checks for our packages
builtins.mapAttrs (_: drv: haskell.lib.dontCheck drv)
(lib.composeExtensions
(haskell.lib.packageSourceOverrides hlsSources) tweaks hself
hsuper));
in {
inherit hlsSources;
# Haskell packages extended with our packages
hlsHpkgs = compiler: extended haskell.packages.${compiler};
# Support of GenChangelogs.hs
gen-hls-changelogs =
let myGHC = haskellPackages.ghcWithPackages (p: with p; [ github ]);
in runCommand "gen-hls-changelogs" {
passAsFile = [ "text" ];
preferLocalBuild = true;
allowSubstitutes = false;
buildInputs = [ git myGHC ];
} ''
dest=$out/bin/gen-hls-changelogs
mkdir -p $out/bin
echo "#!${runtimeShell}" >> $dest
echo "${myGHC}/bin/runghc ${./GenChangelogs.hs}" >> $dest
chmod +x $dest
'';
};
} // (flake-utils.lib.eachSystem [ "x86_64-linux" "x86_64-darwin" ])
(system:
let
pkgs = import nixpkgs {
inherit system;
overlays = [ self.overlay ];
config = { allowBroken = true; };
};
# Pre-commit hooks to run stylish-haskell
pre-commit-check = pre-commit-hooks.lib.${system}.run {
src = ./.;
hooks = {
stylish-haskell.enable = true;
stylish-haskell.excludes = [
# Ignored files
"^Setup.hs$"
"test/testdata/.*$"
"test/data/.*$"
"test/manual/lhs/.*$"
"^hie-compat/.*$"
"^plugins/hls-tactics-plugin/.*$"
# Temporarily ignored files
# Stylish-haskell (and other formatters) does not work well with some CPP usages in these files
"^ghcide/src/Development/IDE/GHC/Compat.hs$"
"^plugins/hls-splice-plugin/src/Ide/Plugin/Splice.hs$"
"^ghcide/test/exe/Main.hs$"
"ghcide/src/Development/IDE/Core/Rules.hs"
"^hls-test-utils/src/Test/Hls/Util.hs$"
];
};
};
# GHC versions
ghcDefault = pkgs.hlsHpkgs ("ghc"
+ pkgs.lib.replaceStrings [ "." ] [ "" ]
pkgs.haskellPackages.ghc.version);
ghc884 = pkgs.hlsHpkgs "ghc884";
ghc8104 = pkgs.hlsHpkgs "ghc8104";
ghc901 = pkgs.hlsHpkgs "ghc901";
# Create a development shell of hls project
# See https://github.com/NixOS/nixpkgs/blob/5d4a430472cafada97888cc80672fab255231f57/pkgs/development/haskell-modules/make-package-set.nix#L319
mkDevShell = hpkgs:
with pkgs;
hpkgs.shellFor {
doBenchmark = true;
packages = p:
with builtins;
map (name: p.${name}) (attrNames pkgs.hlsSources);
buildInputs = [ gmp zlib ncurses capstone tracy gen-hls-changelogs ]
++ (with haskellPackages; [
cabal-install
hlint
ormolu
stylish-haskell
opentelemetry-extra
]);
src = null;
shellHook = ''
export LD_LIBRARY_PATH=${gmp}/lib:${zlib}/lib:${ncurses}/lib:${capstone}/lib
export DYLD_LIBRARY_PATH=${gmp}/lib:${zlib}/lib:${ncurses}/lib:${capstone}/lib
export PATH=$PATH:$HOME/.local/bin
${pre-commit-check.shellHook}
'';
};
# Create a hls executable
# Copied from https://github.com/NixOS/nixpkgs/blob/210784b7c8f3d926b7db73bdad085f4dc5d79418/pkgs/development/tools/haskell/haskell-language-server/withWrapper.nix#L16
mkExe = hpkgs:
with pkgs.haskell.lib;
justStaticExecutables (overrideCabal hpkgs.haskell-language-server
(_: {
postInstall = ''
remove-references-to -t ${hpkgs.ghc} $out/bin/haskell-language-server
remove-references-to -t ${hpkgs.shake.data} $out/bin/haskell-language-server
remove-references-to -t ${hpkgs.js-jquery.data} $out/bin/haskell-language-server
remove-references-to -t ${hpkgs.js-dgtable.data} $out/bin/haskell-language-server
remove-references-to -t ${hpkgs.js-flot.data} $out/bin/haskell-language-server
'';
}));
in with pkgs; rec {
packages = {
# dev shell
haskell-language-server-dev = mkDevShell ghcDefault;
haskell-language-server-884-dev = mkDevShell ghc884;
haskell-language-server-8104-dev = mkDevShell ghc8104;
haskell-language-server-901-dev = mkDevShell ghc901;
# hls package
haskell-language-server = mkExe ghcDefault;
haskell-language-server-884 = mkExe ghc884;
haskell-language-server-8104 = mkExe ghc8104;
haskell-language-server-901 = mkExe ghc901;
};
defaultPackage = packages.haskell-language-server;
devShell = packages.haskell-language-server-dev;
});
}

View File

@ -1,104 +0,0 @@
{ sources ? import ./sources.nix }:
let
nix-pre-commit-hooks = (import (builtins.fetchTarball "https://github.com/cachix/pre-commit-hooks.nix/tarball/87fb108527c7865ebee2ffe9af6154cb761ec9e0/" + "/nix/") { sources = sources; }).packages;
overlay = _self: pkgs:
let
sharedOverrides = {
overrides = _self: super: {
# we override mkDerivation here to apply the following
# tweak to each haskell package:
# if the package is broken, then we disable its check and relax the cabal bounds;
# otherwise, we leave it unchanged.
# hopefully, this could fix packages marked as broken by nix due to check failures
# or the build failure because of tight cabal bounds
mkDerivation = args:
let
broken = args.broken or false;
check = args.doCheck or true;
jailbreak = args.jailbreak or false;
in super.mkDerivation (args // {
jailbreak = if broken then true else jailbreak;
doCheck = if broken then false else check;
});
};
};
ourSources = {
haskell-language-server = gitignoreSource ../.;
ghcide = gitignoreSource ../ghcide;
hls-graph = gitignoreSource ../hls-graph;
shake-bench = gitignoreSource ../shake-bench;
hie-compat = gitignoreSource ../hie-compat;
hls-plugin-api = gitignoreSource ../hls-plugin-api;
hls-test-utils = gitignoreSource ../hls-test-utils;
hls-brittany-plugin = gitignoreSource ../plugins/hls-brittany-plugin;
hls-stylish-haskell-plugin = gitignoreSource ../plugins/hls-stylish-haskell-plugin;
hls-fourmolu-plugin = gitignoreSource ../plugins/hls-fourmolu-plugin;
hls-ormolu-plugin = gitignoreSource ../plugins/hls-ormolu-plugin;
hls-class-plugin = gitignoreSource ../plugins/hls-class-plugin;
hls-haddock-comments-plugin = gitignoreSource ../plugins/hls-haddock-comments-plugin;
hls-eval-plugin = gitignoreSource ../plugins/hls-eval-plugin;
hls-explicit-imports-plugin = gitignoreSource ../plugins/hls-explicit-imports-plugin;
hls-refine-imports-plugin = gitignoreSource ../plugins/hls-refine-imports-plugin;
hls-hlint-plugin = gitignoreSource ../plugins/hls-hlint-plugin;
hls-retrie-plugin = gitignoreSource ../plugins/hls-retrie-plugin;
hls-splice-plugin = gitignoreSource ../plugins/hls-splice-plugin;
hls-tactics-plugin = gitignoreSource ../plugins/hls-tactics-plugin;
hls-floskell-plugin = gitignoreSource ../plugins/hls-floskell-plugin;
hls-pragmas-plugin = gitignoreSource ../plugins/hls-pragmas-plugin;
hls-module-name-plugin = gitignoreSource ../plugins/hls-module-name-plugin;
};
gitignoreSource = (import sources.gitignore { inherit (pkgs) lib; }).gitignoreSource;
extended = haskellPackages:
haskellPackages.extend (pkgs.haskell.lib.packageSourceOverrides ourSources);
in
{
inherit gitignoreSource;
inherit ourSources;
gen-hls-changelogs = with pkgs;
let myGHC = haskellPackages.ghcWithPackages (p: with p; [ github ]);
in runCommand "gen-hls-changelogs" {
passAsFile = [ "text" ];
preferLocalBuild = true;
allowSubstitutes = false;
buildInputs = [ git myGHC ];
} ''
dest=$out/bin/gen-hls-changelogs
mkdir -p $out/bin
echo "#!${runtimeShell}" >> $dest
echo "${myGHC}/bin/runghc ${../GenChangelogs.hs}" >> $dest
chmod +x $dest
'';
ourHaskell = pkgs.haskell // {
packages = pkgs.haskell.packages // {
# relax upper bounds on ghc 8.10.x versions (and skip running tests)
ghc8102 = extended (pkgs.haskell.packages.ghc8102.override sharedOverrides);
ghc8103 = extended (pkgs.haskell.packages.ghc8103.override sharedOverrides);
ghc8104 = extended (pkgs.haskell.packages.ghc8104.override sharedOverrides);
};
};
};
in (import sources.nixpkgs
{
overlays = [ overlay ];
config = {allowBroken = true;};
}) // {
pre-commit-check = nix-pre-commit-hooks.run {
src = ./.;
# If your hooks are intrusive, avoid running on each commit with a default_states like this:
# default_stages = ["manual" "push"];
hooks = {
stylish-haskell.enable = true;
stylish-haskell.excludes = [
# Ignored files
"^Setup.hs$" "test/testdata/.*$" "test/data/.*$" "test/manual/lhs/.*$" "^hie-compat/.*$" "^plugins/hls-tactics-plugin/.*$"
# Temporarily ignored files
# Stylish-haskell (and other formatters) does not work well with some CPP usages in these files
"^ghcide/src/Development/IDE/GHC/Compat.hs$" "^plugins/hls-splice-plugin/src/Ide/Plugin/Splice.hs$" "^ghcide/test/exe/Main.hs$" "ghcide/src/Development/IDE/Core/Rules.hs" "^hls-test-utils/src/Test/Hls/Util.hs$"
];
};
};
}

View File

@ -1,38 +0,0 @@
{
"gitignore": {
"branch": "master",
"description": "Nix function for filtering local git sources",
"homepage": "",
"owner": "hercules-ci",
"repo": "gitignore",
"rev": "211907489e9f198594c0eb0ca9256a1949c9d412",
"sha256": "06j7wpvj54khw0z10fjyi31kpafkr6hi1k0di13k1xp8kywvfyx8",
"type": "tarball",
"url": "https://github.com/hercules-ci/gitignore/archive/211907489e9f198594c0eb0ca9256a1949c9d412.tar.gz",
"url_template": "https://github.com/<owner>/<repo>/archive/<rev>.tar.gz"
},
"niv": {
"branch": "master",
"description": "Easy dependency management for Nix projects",
"homepage": "https://github.com/nmattia/niv",
"owner": "nmattia",
"repo": "niv",
"rev": "af958e8057f345ee1aca714c1247ef3ba1c15f5e",
"sha256": "1qjavxabbrsh73yck5dcq8jggvh3r2jkbr6b5nlz5d9yrqm9255n",
"type": "tarball",
"url": "https://github.com/nmattia/niv/archive/af958e8057f345ee1aca714c1247ef3ba1c15f5e.tar.gz",
"url_template": "https://github.com/<owner>/<repo>/archive/<rev>.tar.gz"
},
"nixpkgs": {
"branch": "nixpkgs-unstable",
"description": "A read-only mirror of NixOS/nixpkgs tracking the released channels. Send issues and PRs to",
"homepage": "https://github.com/NixOS/nixpkgs",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "6fc2b7ecc2a167ce6a6902d5417daf1fa5cac777",
"sha256": "0bcf8yr8scgk7kdjhbrvv7l1d1yv8fnb1d7k3vgnd9qrjnl2fbcf",
"type": "tarball",
"url": "https://github.com/NixOS/nixpkgs/archive/6fc2b7ecc2a167ce6a6902d5417daf1fa5cac777.tar.gz",
"url_template": "https://github.com/<owner>/<repo>/archive/<rev>.tar.gz"
}
}

View File

@ -1,174 +0,0 @@
# This file has been generated by Niv.
let
#
# The fetchers. fetch_<type> fetches specs of type <type>.
#
fetch_file = pkgs: name: spec:
let
name' = sanitizeName name + "-src";
in
if spec.builtin or true then
builtins_fetchurl { inherit (spec) url sha256; name = name'; }
else
pkgs.fetchurl { inherit (spec) url sha256; name = name'; };
fetch_tarball = pkgs: name: spec:
let
name' = sanitizeName name + "-src";
in
if spec.builtin or true then
builtins_fetchTarball { name = name'; inherit (spec) url sha256; }
else
pkgs.fetchzip { name = name'; inherit (spec) url sha256; };
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`!";
in
builtins.fetchGit { url = spec.repo; inherit (spec) rev; inherit ref; };
fetch_local = spec: spec.path;
fetch_builtin-tarball = name: throw
''[${name}] The niv type "builtin-tarball" is deprecated. You should instead use `builtin = true`.
$ niv modify ${name} -a type=tarball -a builtin=true'';
fetch_builtin-url = name: throw
''[${name}] The niv type "builtin-url" will soon be deprecated. You should instead use `builtin = true`.
$ niv modify ${name} -a type=file -a builtin=true'';
#
# Various helpers
#
# https://github.com/NixOS/nixpkgs/pull/83241/files#diff-c6f540a4f3bfa4b0e8b6bafd4cd54e8bR695
sanitizeName = name:
(
concatMapStrings (s: if builtins.isList s then "-" else s)
(
builtins.split "[^[:alnum:]+._?=-]+"
((x: builtins.elemAt (builtins.match "\\.*(.*)" x) 0) name)
)
);
# The set of packages used when specs are fetched using non-builtins.
mkPkgs = sources: system:
let
sourcesNixpkgs =
import (builtins_fetchTarball { inherit (sources.nixpkgs) url sha256; }) { inherit system; };
hasNixpkgsPath = builtins.any (x: x.prefix == "nixpkgs") builtins.nixPath;
hasThisAsNixpkgsPath = <nixpkgs> == ./.;
in
if builtins.hasAttr "nixpkgs" sources
then sourcesNixpkgs
else if hasNixpkgsPath && ! hasThisAsNixpkgsPath then
import <nixpkgs> {}
else
abort
''
Please specify either <nixpkgs> (through -I or NIX_PATH=nixpkgs=...) or
add a package called "nixpkgs" to your sources.json.
'';
# The actual fetching function.
fetch = pkgs: name: spec:
if ! builtins.hasAttr "type" spec then
abort "ERROR: niv spec ${name} does not have a 'type' attribute"
else if spec.type == "file" then fetch_file pkgs name spec
else if spec.type == "tarball" then fetch_tarball pkgs name spec
else if spec.type == "git" then fetch_git name spec
else if spec.type == "local" then fetch_local spec
else if spec.type == "builtin-tarball" then fetch_builtin-tarball name
else if spec.type == "builtin-url" then fetch_builtin-url name
else
abort "ERROR: niv spec ${name} has unknown type ${builtins.toJSON spec.type}";
# If the environment variable NIV_OVERRIDE_${name} is set, then use
# 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;
ersatz = builtins.getEnv "NIV_OVERRIDE_${saneName}";
in
if ersatz == "" then drv else
# this turns the string into an actual Nix path (for both absolute and
# relative paths)
if builtins.substring 0 1 ersatz == "/" then /. + ersatz else /. + builtins.getEnv "PWD" + "/${ersatz}";
# Ports of functions for older nix versions
# a Nix version of mapAttrs if the built-in doesn't exist
mapAttrs = builtins.mapAttrs or (
f: set: with builtins;
listToAttrs (map (attr: { name = attr; value = f attr set.${attr}; }) (attrNames set))
);
# https://github.com/NixOS/nixpkgs/blob/0258808f5744ca980b9a1f24fe0b1e6f0fecee9c/lib/lists.nix#L295
range = first: last: if first > last then [] else builtins.genList (n: first + n) (last - first + 1);
# https://github.com/NixOS/nixpkgs/blob/0258808f5744ca980b9a1f24fe0b1e6f0fecee9c/lib/strings.nix#L257
stringToCharacters = s: map (p: builtins.substring p 1 s) (range 0 (builtins.stringLength s - 1));
# https://github.com/NixOS/nixpkgs/blob/0258808f5744ca980b9a1f24fe0b1e6f0fecee9c/lib/strings.nix#L269
stringAsChars = f: s: concatStrings (map f (stringToCharacters s));
concatMapStrings = f: list: concatStrings (map f list);
concatStrings = builtins.concatStringsSep "";
# https://github.com/NixOS/nixpkgs/blob/8a9f58a375c401b96da862d969f66429def1d118/lib/attrsets.nix#L331
optionalAttrs = cond: as: if cond then as else {};
# fetchTarball version that is compatible between all the versions of Nix
builtins_fetchTarball = { url, name ? null, sha256 }@attrs:
let
inherit (builtins) lessThan nixVersion fetchTarball;
in
if lessThan nixVersion "1.12" then
fetchTarball ({ inherit url; } // (optionalAttrs (!isNull name) { inherit name; }))
else
fetchTarball attrs;
# fetchurl version that is compatible between all the versions of Nix
builtins_fetchurl = { url, name ? null, sha256 }@attrs:
let
inherit (builtins) lessThan nixVersion fetchurl;
in
if lessThan nixVersion "1.12" then
fetchurl ({ inherit url; } // (optionalAttrs (!isNull name) { inherit name; }))
else
fetchurl attrs;
# Create the final "sources" from the config
mkSources = config:
mapAttrs (
name: spec:
if builtins.hasAttr "outPath" spec
then abort
"The values in sources.json should not have an 'outPath' attribute"
else
spec // { outPath = replace name (fetch config.pkgs name spec); }
) config.sources;
# 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)
, system ? builtins.currentSystem
, pkgs ? mkPkgs sources system
}: rec {
# The sources, i.e. the attribute set of spec name to spec
inherit sources;
# The "pkgs" (evaluated nixpkgs) to use for e.g. non-builtin fetchers
inherit pkgs;
};
in
mkSources (mkConfig {}) // { __functor = _: settings: mkSources (mkConfig settings); }

View File

@ -1,56 +0,0 @@
# This shell.nix file is designed for use with cabal build
# It does **not** aim to replace Cabal
# Maintaining this file:
#
# - Bump the nixpkgs version using `niv update nixpkgs`
# - Edit `ourSources` (in nix/default.nix) to update the set of local packages
#
# For more details: https://github.com/NixOS/nixpkgs/blob/20.03/pkgs/development/haskell-modules/make-package-set.nix#L256
{ compiler ? "default",
withHoogle ? false,
nixpkgs ? import ./nix {}
}:
with nixpkgs;
let defaultCompiler = "ghc" + lib.replaceStrings ["."] [""] haskellPackages.ghc.version;
haskellPackagesForProject =
if compiler == "default"
then ourHaskell.packages.${defaultCompiler}
else ourHaskell.packages.${compiler};
packages = p: with builtins; map (name: p.${name}) (attrNames ourSources);
isSupported = compiler == "default" || compiler == defaultCompiler;
in
haskellPackagesForProject.shellFor {
inherit withHoogle;
doBenchmark = true;
packages = p: if isSupported then packages p else [p.ghc-paths];
buildInputs = [
gmp
zlib
ncurses
capstone
tracy
gen-hls-changelogs
haskellPackages.cabal-install
haskellPackages.hlint
haskellPackages.ormolu
haskellPackages.stylish-haskell
haskellPackages.opentelemetry-extra
];
src = null;
shellHook = ''
export LD_LIBRARY_PATH=${gmp}/lib:${zlib}/lib:${ncurses}/lib:${capstone}/lib
export DYLD_LIBRARY_PATH=${gmp}/lib:${zlib}/lib:${ncurses}/lib:${capstone}/lib
export PATH=$PATH:$HOME/.local/bin
${pre-commit-check.shellHook}
'';
}