Skip to content

Commit

Permalink
flake improvements.
Browse files Browse the repository at this point in the history
  • Loading branch information
craigem authored and jbgi committed Apr 27, 2021
1 parent 314c3a5 commit 09ff59e
Show file tree
Hide file tree
Showing 20 changed files with 229 additions and 429 deletions.
3 changes: 0 additions & 3 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1,4 +1 @@
# GitHub Linguist annotations.

# Hide niv boilerplate
nix/sources.nix linguist-generated=true
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ tags
# For now require that users generate their own hie.yaml
hie.yaml

# Ignore local modification of nix config files
/custom-config.nix
/workbench-config.nix
/supported-systems.nix

# Ignore Visual Studio code configuration
.vscode/tasks.json

Expand Down
1 change: 1 addition & 0 deletions custom-config.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ }
25 changes: 4 additions & 21 deletions default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
# allows to cutomize haskellNix (ghc and profiling, see ./nix/haskell.nix)
, config ? {}
# override scripts with custom configuration
, customConfig ? {}
, customConfig ? import ./custom-config.nix
# allows to override dependencies of the project without modifications,
# eg. to test build against local checkout of nixpkgs and iohk-nix:
# nix build -f default.nix cardano-node --arg sourcesOverride '{
# iohk-nix = ../iohk-nix;
# }'
, sourcesOverride ? {}
# pinned version of nixpkgs augmented with overlays (iohk-nix and our packages).
, pkgs ? import ./nix/default.nix { inherit system crossSystem config sourcesOverride gitrev; }
, pkgs ? import ./nix/default.nix { inherit system crossSystem config customConfig sourcesOverride gitrev; }
# Git sha1 hash, to be passed when not building from a git work tree.
, gitrev ? null
}:
Expand All @@ -22,7 +22,6 @@ let
# the Haskell.nix package set, reduced to local packages.
(selectProjectPackages cardanoNodeHaskellPackages);

scripts = callPackage ./nix/scripts.nix { inherit customConfig; };
# NixOS tests run a proxy and validate it listens
nixosTests = recRecurseIntoAttrs (import ./nix/nixos/tests {
inherit pkgs;
Expand All @@ -40,34 +39,18 @@ let
scripts = callPackage ./nix/scripts.nix { customConfig = customConfig'; };
};

rewrite-static = _: p: if (pkgs.stdenv.hostPlatform.isDarwin) then
pkgs.runCommandCC p.name {
nativeBuildInputs = [ pkgs.haskellBuildUtils.package pkgs.buildPackages.binutils pkgs.buildPackages.nix ];
} ''
cp -R ${p} $out
chmod -R +w $out
rewrite-libs $out/bin $out/bin/*
'' else if (pkgs.stdenv.hostPlatform.isMusl) then
pkgs.runCommandCC p.name { } ''
cp -R ${p} $out
chmod -R +w $out
$STRIP $out/bin/*
'' else p;

packages = {
inherit haskellPackages
cardano-node cardano-node-profiled cardano-node-eventlogged
cardano-cli db-converter cardano-ping
scripts nixosTests environments dockerImage mkCluster bech32;

# so that eval time gc roots are cached (nix-tools stuff)
inherit (cardanoNodeHaskellPackages) roots;
inherit (cardanoNodeProject) roots plan-nix;

inherit (haskellPackages.cardano-node.identifier) version;

inherit (haskellPackages.cardano-node.project) plan-nix;

exes = mapAttrsRecursiveCond (as: !(isDerivation as)) rewrite-static (collectComponents' "exes" haskellPackages);
exes = mapAttrsRecursiveCond (as: !(isDerivation as)) rewriteStatic (collectComponents' "exes" haskellPackages);

# `tests` are the test suites which have been built.
tests = collectComponents' "tests" haskellPackages;
Expand Down
54 changes: 29 additions & 25 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

175 changes: 87 additions & 88 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -2,100 +2,99 @@
description = "Cardano Node";

inputs = {
haskell-nix.url = "github:input-output-hk/haskell.nix";
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
haskellNix.url = "github:input-output-hk/haskell.nix";
# Would rather use iohkNix/nixpkgs here but we may need to add a flake there first
nixpkgs.follows = "haskellNix/nixpkgs-unstable";
utils.url = "github:numtide/flake-utils";
iohkNix.url = "github:input-output-hk/iohk-nix/flake";
};

outputs = { self, nixpkgs, utils, haskell-nix, ... }:
(utils.lib.eachSystem [ "x86_64-linux" "x86_64-darwin" ] (system:
let
legacyPackages = import ./nix {
inherit sources system nixpkgs iohkNix;
haskellNix = haskell-nix.legacyPackages.${system};
outputs = { self, nixpkgs, utils, haskellNix, iohkNix, ... }:
let
inherit (haskellNix.internal) config;
inherit (utils.lib) eachSystem mkApp flattenTree;

overlays = with iohkNix.overlays; [
haskellNix.overlay
haskell-nix-extra
crypto
cardano-lib
(final: prev: {
customConfig = import ./custom-config.nix;
workbenchConfig = import ./workbench-config.nix;
gitrev = self.rev or "dirty";
};
lib = nixpkgs.lib;
sources = import ./nix/sources.nix { pkgs = legacyPackages; };
iohkNix = import sources.iohk-nix { inherit system; };
environments = iohkNix.cardanoLib.environments;
environmentName = "testnet";

eachEnv = lib.flip lib.pipe [
(lib.forEach (builtins.attrNames environments))
lib.listToAttrs
];

config = env:
{ pkgs, ... }: {
services.cardano-node = rec {
stateDir = "/persist";
socketPath = "/alloc/node.socket";
enable = true;
package = pkgs.cardano-node;
environment = env;
cardanoNodePkgs = pkgs;
hostAddr = "0.0.0.0";
port = 3001;
};
};
commonLib = final.lib // final.cardanoLib;
})
(import ./nix/pkgs.nix)
];

evaluated = env:
lib.nixosSystem {
inherit system;
pkgs = legacyPackages;
modules = [ ./nix/nixos/cardano-node-service.nix (config env) ];
};
in eachSystem (import ./supported-systems.nix) (system:
let
pkgs = import nixpkgs { inherit system overlays config; };

inherit (pkgs.lib) systems mapAttrs mapAttrs' nameValuePair attrValues
recursiveUpdate mapAttrsRecursiveCond isDerivation concatStringsSep
mkDefault optionalAttrs;

devShell = import ./shell.nix { inherit pkgs; };

flake = pkgs.cardanoNodeProject.flake {};
muslFlake = (import nixpkgs { inherit system overlays config;
crossSystem = systems.examples.musl64;
}).cardanoNodeProject.flake {};
windowsFlake = (import nixpkgs { inherit system overlays config;
crossSystem = systems.examples.mingwW64;
}).cardanoNodeProject.flake {};

prefixNamesWith = p: mapAttrs' (n: v: nameValuePair "${p}${n}" v);

packages = let
deps = with legacyPackages; [
coreutils
findutils
gnugrep
gnused
postgresql
strace
lsof
dnsutils
bashInteractive
iproute
curl
netcat
bat
tree
];

vanilla = eachEnv (env:
lib.nameValuePair "cardano-node-${env}"
(legacyPackages.writeShellScriptBin "cardano-node-entrypoint" ''
${(evaluated env).config.services.cardano-node.script}
''));

debug = eachEnv (env:
let
entrypoint =
legacyPackages.writeShellScriptBin "cardano-node-entrypoint" ''
${(evaluated env).config.services.cardano-node.script}
'';

closure = legacyPackages.symlinkJoin {
name = "cardano-node-entrypoint";
paths = [ entrypoint ] ++ deps;
};
in lib.nameValuePair "cardano-node-${env}-debug" closure);
in debug // vanilla;
in {
inherit iohkNix environments evaluated legacyPackages packages;

apps = eachEnv (env:
lib.nameValuePair "cardano-node-${env}" (utils.lib.mkApp {
drv = packages."cardano-node-${env}";
exePath = "/bin/cardano-node-entrypoint";
}));
})) // {
overlay = import ./overlay.nix self;
nixosModules = {
cardano-node = { imports = [ ./nix/nixos/cardano-node-service.nix ]; };
in recursiveUpdate flake {

legacyPackages = pkgs;

packages = { inherit (devShell) devops; }
// optionalAttrs (system == "x86_64-linux") (
prefixNamesWith "windows:" windowsFlake.packages
// (prefixNamesWith "static:" muslFlake.packages)
);

checks = prefixNamesWith "windows:" windowsFlake.checks;

# Built by `nix build .`
defaultPackage = flake.packages."cardano-node:exe:cardano-node";

# Run by `nix run .`
defaultApp = flake.apps."cardano-node:exe:cardano-node";

# This is used by `nix develop .` to open a devShell
inherit devShell;

apps = {
repl = mkApp {
drv = pkgs.writeShellScriptBin "repl" ''
confnix=$(mktemp)
echo "builtins.getFlake (toString $(git rev-parse --show-toplevel))" >$confnix
trap "rm $confnix" EXIT
nix repl $confnix
'';
};
} // (mapAttrs
# nix run .#<env>/node
(_: drv: (mkApp {inherit drv; exePath = "";}))
(flattenTree pkgs.scripts));

}
) // {
overlay = import ./overlay.nix self;
nixosModules = {
cardano-node = { config, pkgs, lib, ... }: {
imports = [ ./nix/nixos/cardano-node-service.nix ];
config.services.cardano-node.cardanoNodePkgs = lib.mkDefault self.legacyPackages.${pkgs.system};
};
cardano-submit-api = { config, pkgs, lib, ... }: {
imports = [ ./nix/nixos/cardano-submit-api-service.nix ];
config.services.cardano-submit-api.cardanoNodePkgs = lib.mkDefault self.legacyPackages.${pkgs.system};
};
};
};
}
Loading

0 comments on commit 09ff59e

Please sign in to comment.