Skip to content

Commit

Permalink
nix: refatored how shell & tools are provided
Browse files Browse the repository at this point in the history
  • Loading branch information
coot committed Sep 28, 2024
1 parent 0d462a3 commit eef86fb
Show file tree
Hide file tree
Showing 5 changed files with 158 additions and 89 deletions.
45 changes: 27 additions & 18 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,35 @@
overlays = [
# haskellNix.overlay can be configured by later overlays, so need to come before them.
inputs.haskellNix.overlay
(import ./nix/tools.nix inputs)
(import ./nix/ouroboros-network.nix inputs)
(import ./nix/network-docs.nix inputs)
];
};
inherit (pkgs) lib;

flake = pkgs.ouroboros-network.flake {};
network-docs = pkgs.callPackage ./nix/network-docs.nix { };
check-stylish = pkgs.callPackage ./nix/check-stylish.nix { };
inherit (pkgs) lib network-docs;

# shells accessible through `nix develop`,
# `nix develop .\#devShells.x86_64-linux.ghc810`, etc.
devShells = rec {
default = import ./nix/shell.nix { hls = true;
inherit inputs pkgs;
ouroboros-network = pkgs.ouroboros-network; };
profiled = import ./nix/shell.nix { hls = true;
inherit inputs pkgs;
ouroboros-network = pkgs.ouroboros-network.projectVariants.profiled; };
ghc810 = import ./nix/shell.nix { hls = false; # hls-2.7.0.0 cannot be compiled for `ghc-8.10`
inherit inputs pkgs;
ouroboros-network = pkgs.ouroboros-network.projectVariants.ghc810; };
ghc810-profiled
= import ./nix/shell.nix { hls = false;
inherit inputs pkgs;
ouroboros-network = pkgs.ouroboros-network.projectVariants.ghc810-profiled; };
};

# jobs executed on hydra
hydraJobs =
pkgs.callPackages inputs.iohkNix.utils.ciJobsAggregates
{
Expand All @@ -62,34 +83,22 @@
# This ensure hydra send a status for the required job (even if no change other than commit hash)
revision = pkgs.writeText "revision" (inputs.self.rev or "dirty");
inherit network-docs check-stylish;
devShell = devShells.default;
};
}
# add network-docs & check-stylish to support
# `nix build .\#hydraJobs.x86_64-linux.network-docs` and
# `nix build .\#hydraJobs.x86_64-linux.check-stylis`.
// { inherit network-docs check-stylish; };
# also provide hydraJobs through legacyPackages to allow building without system prefix, e.g.

# Provide hydraJobs through legacyPackages to allow building without system prefix, e.g.
# `nix build .\#network-mux:lib:network-mux`
# `nix build .\#network-docs`
legacyPackages = { inherit hydraJobs network-docs check-stylish; };
in
lib.recursiveUpdate flake rec {
project = pkgs.ouroboros-network;
inherit hydraJobs;
inherit legacyPackages;
devShells = let
profillingShell = p: {
# `nix develop .#profiling`
profiling = (p.appendModule {modules = [{enableLibraryProfiling = true;}];}).shell;
};
in
profillingShell pkgs.ouroboros-network
# Additional shells for every GHC version supported by haskell.nix, eg. `nix develop .#ghc927`
// lib.mapAttrs (compiler-nix-name: _: let
p = pkgs.ouroboros-network.appendModule {inherit compiler-nix-name;};
in
p.shell // (profillingShell p))
pkgs.haskell-nix.compiler;
inherit hydraJobs legacyPackages devShells;
# formatter used by nix fmt
formatter = pkgs.alejandra;
}
Expand Down
86 changes: 40 additions & 46 deletions nix/network-docs.nix
Original file line number Diff line number Diff line change
@@ -1,53 +1,47 @@
{ pkgs }:
with pkgs;
let
src = haskell-nix.haskellLib.cleanGit {
name = "network-docs";
inputs: final: prev: {
network-docs = final.stdenvNoCC.mkDerivation {
name = "ouroboros-network-docs";
src = ../.;
subDir = "docs";
};
cddl-specs = ../ouroboros-network-protocols/test-cddl/specs;
in pkgs.runCommand "network-docs" {
meta.platforms = with pkgs.lib.platforms; [ linux darwin ];
nativeBuildInputs = [ imagemagick ];
buildInputs = [
(texlive.combine {
inherit (texlive)
cleveref framed scheme-small collection-fontsrecommended stmaryrd
kpfonts geometry hyperref todonotes amsmath mathtools colortbl polytable
lazylist fancyvrb
#graphicx
pstricks wrapfig
# build tools
latexmk;
})
];
} ''
for d in network-design network-spec; do
mkdir -p docs/$d
ln -s ${src}/$d/* docs/$d/
done
nativeBuildInputs = [
(final.texlive.combine {
inherit (final.texlive)
collection-latexextra
collection-latexrecommended
collection-mathscience
latexmk;
})
];
buildPhase =
let src = ../.;
cddl-specs = ../ouroboros-network-protocols/test-cddl/specs; in
''
for d in network-design network-spec; do
mkdir -p docs/$d
ln -s ${src}/$d/* docs/$d/
done
mkdir -p ouroboros-network-protocols/test-cddl/specs
cp ${cddl-specs}/*.cddl ouroboros-network-protocols/test-cddl/specs
mkdir -p ouroboros-network-protocols/test-cddl/specs
cp ${cddl-specs}/*.cddl ouroboros-network-protocols/test-cddl/specs
mkdir -p $out
mkdir -p $out
(
cd docs/network-design
latexmk -pdf -pdflatex="pdflatex -interaction=nonstopmode"
cp -a *.pdf $out/
)
(
cd docs/network-design
latexmk -pdf -pdflatex="pdflatex -interaction=nonstopmode"
cp -a *.pdf $out/
)
(
cd docs/network-spec
make all
cp -a *.pdf $out/
)
(
cd docs/network-spec
make all
cp -a *.pdf $out/
)
mkdir -p $out/nix-support
mkdir -p $out/nix-support
for pdf in $out/*.pdf; do
echo "file binary-dist $pdf" >> $out/nix-support/hydra-build-products
done
''
for pdf in $out/*.pdf; do
echo "file binary-dist $pdf" >> $out/nix-support/hydra-build-products
done
'';
};
}
53 changes: 28 additions & 25 deletions nix/ouroboros-network.nix
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,16 @@ let
};
};

# Make a profiled variant for a given compiler.
mkProfiledVariant = compiler-nix-name:
{ inherit compiler-nix-name;
modules = [{ enableLibraryProfiling = true;
enableProfiling = true;
# https://well-typed.com/blog/2023/03/prof-late/
profilingDetail = "late";
}];
};

# We use cabalProject' to ensure we don't build the plan for
# all systems.
ouroboros-network = haskell-nix.cabalProject' ({config, pkgs, ...}: {
Expand All @@ -54,43 +64,36 @@ let
# --------

# using different compilers
flake.variants = (lib.genAttrs otherCompilers
(compiler-nix-name: { inherit compiler-nix-name; }));
flake.variants =
# otherCompilers
(lib.genAttrs otherCompilers
(compiler-nix-name: { inherit compiler-nix-name; }))
//
# otherCompilers with profiling enabled
(lib.lists.foldr
(compiler-nix-name: acc:
acc // lib.setAttrByPath [(compiler-nix-name + "-profiled")]
(mkProfiledVariant compiler-nix-name))
{}
otherCompilers
)
//
# default compiler with profiling enabled
{ profiled = mkProfiledVariant defaultCompiler; };

#
# CHaP
# ----

# CHaP input map, so we can find CHaP packages (needs to be more
# recent than the index-state we set!). Can be updated with
# recenbt than the index-state we set!). Can be updated with
#
# nix flake lock --update-input CHaP
#
inputMap = {
"https://chap.intersectmbo.org/" = inputs.CHaP;
};

#
# SHELL
# -----

# tools we want in our shell, from hackage
shell.tools =
{
cabal = "3.12.1.0";
ghcid = "0.8.9";
}
// lib.optionalAttrs (config.compiler-nix-name == defaultCompiler) {
# tools that work only with default compiler
stylish-haskell = "0.14.6.0";
haskell-language-server = "2.7.0.0";
};
# and from nixpkgs or other inputs
shell.nativeBuildInputs = [];
# disable Hoogle until someone request it
shell.withHoogle = false;
# Skip cross compilers for the shell
shell.crossPlatforms = _: [];

#
# MODULES
# -------
Expand Down
42 changes: 42 additions & 0 deletions nix/shell.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{ hls, inputs, pkgs, ouroboros-network }:

let
inherit (pkgs) lib;
in
ouroboros-network.shellFor {
nativeBuildInputs = [
pkgs.cabal
pkgs.fd
pkgs.nixpkgs-fmt
pkgs.stylish-haskell
pkgs.ghcid
(pkgs.texlive.combine {
inherit (pkgs.texlive)
collection-latexextra
collection-latexrecommended
collection-mathscience
latexmk;
})
];

# This is the place for tools that are required to be built with the same GHC
# version as used in ouroboros-network.
tools =
lib.optionalAttrs hls
{
haskell-language-server = {
src = inputs.haskellNix.inputs."hls-2.7";
configureArgs = "--disable-benchmarks --disable-tests";
};
};

shellHook = ''
export LANG="en_US.UTF-8"
'' + lib.optionalString
(pkgs.glibcLocales != null && pkgs.stdenv.hostPlatform.libc == "glibc") ''
export LOCALE_ARCHIVE="${pkgs.glibcLocales}/lib/locale/locale-archive"
'';

withHoogle = true;
}

21 changes: 21 additions & 0 deletions nix/tools.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
inputs: final: prev:

let
inherit (final) lib;
tool-index-state = "2024-07-04T00:00:00Z";
tool = name: version: other:
final.haskell-nix.tool final.ouroboros-network.args.compiler-nix-name name ({
version = version;
index-state = tool-index-state;
} // other);
in
{
inherit tool-index-state;
cabal = tool "cabal" "3.12.1.0" { };
stylish-haskell = tool "stylish-haskell" "0.14.6.0" { };
haskellBuildUtils = prev.haskellBuildUtils.override {
inherit (final.ouroboros-network.args) compiler-nix-name;
index-state = tool-index-state;
};
}

0 comments on commit eef86fb

Please sign in to comment.