From 302ea8ce6e5b143079bcef2a9e0bc8b7e96dd0f0 Mon Sep 17 00:00:00 2001 From: "Alexander V. Nikolaev" Date: Mon, 16 Oct 2023 18:49:28 +0300 Subject: [PATCH 01/21] feat: project skeleton, devshell --- .envrc | 5 + checks/default.nix | 38 ++++++ ci/default.nix | 3 + flake.lock | 299 ++++++++++++++++++++++++++++++++++++++++++ flake.nix | 50 +++++++ formatter/default.nix | 29 ++++ modules/default.nix | 10 ++ shell/default.nix | 13 ++ statix.toml | 3 + 9 files changed, 450 insertions(+) create mode 100644 .envrc create mode 100644 checks/default.nix create mode 100644 ci/default.nix create mode 100644 flake.lock create mode 100644 flake.nix create mode 100644 formatter/default.nix create mode 100644 modules/default.nix create mode 100644 shell/default.nix create mode 100644 statix.toml diff --git a/.envrc b/.envrc new file mode 100644 index 0000000..6b2c0d7 --- /dev/null +++ b/.envrc @@ -0,0 +1,5 @@ +if ! has nix_direnv_version || ! nix_direnv_version 2.2.0; then + source_url "https://raw.githubusercontent.com/nix-community/nix-direnv/2.2.0/direnvrc" "sha256-5EwyKnkJNQeXrRkYbwwRBcXbibosCJqyIUuz9Xq+LRc=" +fi +nix_direnv_watch_file ./shell/default.nix +use flake diff --git a/checks/default.nix b/checks/default.nix new file mode 100644 index 0000000..0bb3895 --- /dev/null +++ b/checks/default.nix @@ -0,0 +1,38 @@ +{inputs, ...}: { + perSystem = { + pkgs, + config, + ... + }: { + checks = let + devour-flake = pkgs.callPackage inputs.devour-flake {}; + in + { + nix-build-all = pkgs.writeShellApplication { + name = "nix-build-all"; + runtimeInputs = [ + pkgs.nix + devour-flake + ]; + text = '' + # Make sure that flake.lock is sync + nix flake lock --no-update-lock-file + + # Do a full nix build (all outputs) + devour-flake . "$@" + ''; + }; + } + # mix in tests + // config.testing.checks; + + devshells.default.commands = [ + { + category = "Tools"; + name = "check"; + help = "Checks the source tree"; + command = "nix flake check"; + } + ]; + }; +} diff --git a/ci/default.nix b/ci/default.nix new file mode 100644 index 0000000..4d84f43 --- /dev/null +++ b/ci/default.nix @@ -0,0 +1,3 @@ +{ + flake.herculesCI.ciSystems = ["x86_64-linux"]; +} diff --git a/flake.lock b/flake.lock new file mode 100644 index 0000000..3a55c3c --- /dev/null +++ b/flake.lock @@ -0,0 +1,299 @@ +{ + "nodes": { + "devour-flake": { + "flake": false, + "locked": { + "lastModified": 1694098737, + "narHash": "sha256-O51F4YFOzlaQAc9b6xjkAqpvrvCtw/Os2M7TU0y4SKQ=", + "owner": "srid", + "repo": "devour-flake", + "rev": "30a34036b29b0d12989ef6c8be77aa949d85aef5", + "type": "github" + }, + "original": { + "owner": "srid", + "repo": "devour-flake", + "type": "github" + } + }, + "devshell": { + "inputs": { + "nixpkgs": [ + "nixpkgs" + ], + "systems": "systems" + }, + "locked": { + "lastModified": 1695973661, + "narHash": "sha256-BP2H4c42GThPIhERtTpV1yCtwQHYHEKdRu7pjrmQAwo=", + "owner": "numtide", + "repo": "devshell", + "rev": "cd4e2fda3150dd2f689caeac07b7f47df5197c31", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "devshell", + "type": "github" + } + }, + "flake-parts": { + "inputs": { + "nixpkgs-lib": "nixpkgs-lib" + }, + "locked": { + "lastModified": 1696343447, + "narHash": "sha256-B2xAZKLkkeRFG5XcHHSXXcP7To9Xzr59KXeZiRf4vdQ=", + "owner": "hercules-ci", + "repo": "flake-parts", + "rev": "c9afaba3dfa4085dbd2ccb38dfade5141e33d9d4", + "type": "github" + }, + "original": { + "owner": "hercules-ci", + "repo": "flake-parts", + "type": "github" + } + }, + "flake-parts_2": { + "inputs": { + "nixpkgs-lib": "nixpkgs-lib_2" + }, + "locked": { + "lastModified": 1688466019, + "narHash": "sha256-VeM2akYrBYMsb4W/MmBo1zmaMfgbL4cH3Pu8PGyIwJ0=", + "owner": "hercules-ci", + "repo": "flake-parts", + "rev": "8e8d955c22df93dbe24f19ea04f47a74adbdc5ec", + "type": "github" + }, + "original": { + "id": "flake-parts", + "type": "indirect" + } + }, + "flake-parts_3": { + "inputs": { + "nixpkgs-lib": [ + "hercules-ci-effects", + "hercules-ci-agent", + "nixpkgs" + ] + }, + "locked": { + "lastModified": 1688466019, + "narHash": "sha256-VeM2akYrBYMsb4W/MmBo1zmaMfgbL4cH3Pu8PGyIwJ0=", + "owner": "hercules-ci", + "repo": "flake-parts", + "rev": "8e8d955c22df93dbe24f19ea04f47a74adbdc5ec", + "type": "github" + }, + "original": { + "owner": "hercules-ci", + "repo": "flake-parts", + "type": "github" + } + }, + "flake-root": { + "locked": { + "lastModified": 1692742795, + "narHash": "sha256-f+Y0YhVCIJ06LemO+3Xx00lIcqQxSKJHXT/yk1RTKxw=", + "owner": "srid", + "repo": "flake-root", + "rev": "d9a70d9c7a5fd7f3258ccf48da9335e9b47c3937", + "type": "github" + }, + "original": { + "owner": "srid", + "repo": "flake-root", + "type": "github" + } + }, + "haskell-flake": { + "locked": { + "lastModified": 1684780604, + "narHash": "sha256-2uMZsewmRn7rRtAnnQNw1lj0uZBMh4m6Cs/7dV5YF08=", + "owner": "srid", + "repo": "haskell-flake", + "rev": "74210fa80a49f1b6f67223debdbf1494596ff9f2", + "type": "github" + }, + "original": { + "owner": "srid", + "ref": "0.3.0", + "repo": "haskell-flake", + "type": "github" + } + }, + "hercules-ci-agent": { + "inputs": { + "flake-parts": "flake-parts_3", + "haskell-flake": "haskell-flake", + "nixpkgs": "nixpkgs" + }, + "locked": { + "lastModified": 1688568579, + "narHash": "sha256-ON0M56wtY/TIIGPkXDlJboAmuYwc73Hi8X9iJGtxOhM=", + "owner": "hercules-ci", + "repo": "hercules-ci-agent", + "rev": "367dd8cd649b57009a6502e878005a1e54ad78c5", + "type": "github" + }, + "original": { + "id": "hercules-ci-agent", + "type": "indirect" + } + }, + "hercules-ci-effects": { + "inputs": { + "flake-parts": "flake-parts_2", + "hercules-ci-agent": "hercules-ci-agent", + "nixpkgs": "nixpkgs_2" + }, + "locked": { + "lastModified": 1697031886, + "narHash": "sha256-oTMPX8dGC7yxSwrbF4NuPNQsUEcHB1dusW2yEbFD5zg=", + "owner": "hercules-ci", + "repo": "hercules-ci-effects", + "rev": "178b36dc3a75c96efc25477d45eafc37ba1fafc3", + "type": "github" + }, + "original": { + "owner": "hercules-ci", + "repo": "hercules-ci-effects", + "type": "github" + } + }, + "nixpkgs": { + "locked": { + "lastModified": 1688322751, + "narHash": "sha256-eW62dC5f33oKZL7VWlomttbUnOTHrAbte9yNUNW8rbk=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "0fbe93c5a7cac99f90b60bdf5f149383daaa615f", + "type": "github" + }, + "original": { + "owner": "NixOS", + "ref": "nixos-unstable", + "repo": "nixpkgs", + "type": "github" + } + }, + "nixpkgs-lib": { + "locked": { + "dir": "lib", + "lastModified": 1696019113, + "narHash": "sha256-X3+DKYWJm93DRSdC5M6K5hLqzSya9BjibtBsuARoPco=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "f5892ddac112a1e9b3612c39af1b72987ee5783a", + "type": "github" + }, + "original": { + "dir": "lib", + "owner": "NixOS", + "ref": "nixos-unstable", + "repo": "nixpkgs", + "type": "github" + } + }, + "nixpkgs-lib_2": { + "locked": { + "dir": "lib", + "lastModified": 1688049487, + "narHash": "sha256-100g4iaKC9MalDjUW9iN6Jl/OocTDtXdeAj7pEGIRh4=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "4bc72cae107788bf3f24f30db2e2f685c9298dc9", + "type": "github" + }, + "original": { + "dir": "lib", + "owner": "NixOS", + "ref": "nixos-unstable", + "repo": "nixpkgs", + "type": "github" + } + }, + "nixpkgs_2": { + "locked": { + "lastModified": 1689393711, + "narHash": "sha256-l3gyTPy/qWLDk1CY1EgYwlsxcGxN2aVd7MlCzQa69rk=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "27fcd46fa18df36d270174246e7bd8f1787129ff", + "type": "github" + }, + "original": { + "owner": "NixOS", + "repo": "nixpkgs", + "type": "github" + } + }, + "nixpkgs_3": { + "locked": { + "lastModified": 1697059129, + "narHash": "sha256-9NJcFF9CEYPvHJ5ckE8kvINvI84SZZ87PvqMbH6pro0=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "5e4c2ada4fcd54b99d56d7bd62f384511a7e2593", + "type": "github" + }, + "original": { + "owner": "NixOS", + "ref": "nixos-unstable", + "repo": "nixpkgs", + "type": "github" + } + }, + "root": { + "inputs": { + "devour-flake": "devour-flake", + "devshell": "devshell", + "flake-parts": "flake-parts", + "flake-root": "flake-root", + "hercules-ci-effects": "hercules-ci-effects", + "nixpkgs": "nixpkgs_3", + "treefmt-nix": "treefmt-nix" + } + }, + "systems": { + "locked": { + "lastModified": 1681028828, + "narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=", + "owner": "nix-systems", + "repo": "default", + "rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e", + "type": "github" + }, + "original": { + "owner": "nix-systems", + "repo": "default", + "type": "github" + } + }, + "treefmt-nix": { + "inputs": { + "nixpkgs": [ + "nixpkgs" + ] + }, + "locked": { + "lastModified": 1697388351, + "narHash": "sha256-63N2eBpKaziIy4R44vjpUu8Nz5fCJY7okKrkixvDQmY=", + "owner": "numtide", + "repo": "treefmt-nix", + "rev": "aae39f64f5ecbe89792d05eacea5cb241891292a", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "treefmt-nix", + "type": "github" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/flake.nix b/flake.nix new file mode 100644 index 0000000..31ef6d5 --- /dev/null +++ b/flake.nix @@ -0,0 +1,50 @@ +{ + inputs = { + nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable"; + flake-parts.url = "github:hercules-ci/flake-parts"; + + # we use effects for CI and documentation + hercules-ci-effects.url = "github:hercules-ci/hercules-ci-effects"; + + # Utilities + devshell = { + url = "github:numtide/devshell"; + inputs.nixpkgs.follows = "nixpkgs"; + }; + flake-root.url = "github:srid/flake-root"; + treefmt-nix = { + url = "github:numtide/treefmt-nix"; + inputs.nixpkgs.follows = "nixpkgs"; + }; + devour-flake = { + url = "github:srid/devour-flake"; + flake = false; + }; + }; + outputs = inputs @ { + flake-parts, + ... + }: + flake-parts.lib.mkFlake { + inherit inputs; + } + { + imports = [ + inputs.devshell.flakeModule + inputs.flake-parts.flakeModules.easyOverlay + inputs.flake-root.flakeModule + inputs.hercules-ci-effects.flakeModule + inputs.treefmt-nix.flakeModule + ./checks + ./ci + ./formatter + ./modules + ]; + systems = [ + "x86_64-linux" + "aarch64-linux" + "x86_64-darwin" + "aarch64-darwin" + ]; + }; +} diff --git a/formatter/default.nix b/formatter/default.nix new file mode 100644 index 0000000..809cf71 --- /dev/null +++ b/formatter/default.nix @@ -0,0 +1,29 @@ +{ + perSystem = { + config, + pkgs, + ... + }: { + treefmt.config = { + inherit (config.flake-root) projectRootFile; + package = pkgs.treefmt; + flakeFormatter = true; + flakeCheck = true; + programs = { + alejandra.enable = true; + deadnix.enable = true; + prettier.enable = true; + statix.enable = true; + }; + }; + + devshells.default.commands = [ + { + category = "Tools"; + name = "fmt"; + help = "Format the source tree"; + command = "nix fmt"; + } + ]; + }; +} diff --git a/modules/default.nix b/modules/default.nix new file mode 100644 index 0000000..ccaa4c3 --- /dev/null +++ b/modules/default.nix @@ -0,0 +1,10 @@ +{ + imports = [ + ]; + + # create a default nixos module which mixes in all modules + flake.nixosModules.default = { + imports = [ + ]; + }; +} diff --git a/shell/default.nix b/shell/default.nix new file mode 100644 index 0000000..0cea60a --- /dev/null +++ b/shell/default.nix @@ -0,0 +1,13 @@ +{ + perSystem = {pkgs, ...}: { + devshells.default = { + name = "cardano.nix"; + packages = with pkgs; [ + nix-update + statix + mkdocs + pkgs.python310Packages.mkdocs-material + ]; + }; + }; +} diff --git a/statix.toml b/statix.toml new file mode 100644 index 0000000..7df5742 --- /dev/null +++ b/statix.toml @@ -0,0 +1,3 @@ +disabled = [ + ".direnv" +] From fbe43a73c6111f76da8ffe9d9c369aa3bd4d03e3 Mon Sep 17 00:00:00 2001 From: "Alexander V. Nikolaev" Date: Tue, 17 Oct 2023 15:49:30 +0300 Subject: [PATCH 02/21] feat: demo module for global settings --- ci/default.nix | 2 +- flake.nix | 5 +---- modules/default.nix | 1 + modules/global/default.nix | 34 ++++++++++++++++++++++++++++++++++ 4 files changed, 37 insertions(+), 5 deletions(-) create mode 100644 modules/global/default.nix diff --git a/ci/default.nix b/ci/default.nix index 4d84f43..b850b87 100644 --- a/ci/default.nix +++ b/ci/default.nix @@ -1,3 +1,3 @@ { - flake.herculesCI.ciSystems = ["x86_64-linux"]; + config.herculesCI.ciSystems = ["x86_64-linux"]; } diff --git a/flake.nix b/flake.nix index 31ef6d5..4ca16fe 100644 --- a/flake.nix +++ b/flake.nix @@ -21,10 +21,7 @@ flake = false; }; }; - outputs = inputs @ { - flake-parts, - ... - }: + outputs = inputs @ {flake-parts, ...}: flake-parts.lib.mkFlake { inherit inputs; } diff --git a/modules/default.nix b/modules/default.nix index ccaa4c3..fa5ac4d 100644 --- a/modules/default.nix +++ b/modules/default.nix @@ -5,6 +5,7 @@ # create a default nixos module which mixes in all modules flake.nixosModules.default = { imports = [ + ./global ]; }; } diff --git a/modules/global/default.nix b/modules/global/default.nix new file mode 100644 index 0000000..2511ab0 --- /dev/null +++ b/modules/global/default.nix @@ -0,0 +1,34 @@ +{ + lib, + config, + ... +}: let + cfg = config.cardano-ecosystem.globals; + # FIXME: fill other nets + # FIXME: could we extract networkNumbers automatically? + # FIXME: could we extract it from IOG nix stuff w/o IFD or massive inclusion of https://github.com/input-output-hk/iohk-nix + networkNumbers = { + mainnet = 0; + private = 42; + }; +in + # FIXME: proper assertion, private also can have any unused number + assert networkNumbers ? cfg.network; + with lib; { + options.cardano-ecosystem.globals = { + network = mkOption { + type = types.enum (builtins.attrNames networkNumbers); + default = "mainnet"; + description = '' + Cardano network to join/use + ''; + }; + networkNumber = mkOption { + type = types.int; + default = networkNumbers.${cfg.network}; + description = '' + Cardano network number to join/use (should match cardano-ecosystem.globals,network) + ''; + }; + }; + } From 527bc37e87af3052b2eb3277117996c517be4a05 Mon Sep 17 00:00:00 2001 From: "Alexander V. Nikolaev" Date: Fri, 20 Oct 2023 20:56:52 +0300 Subject: [PATCH 03/21] feat: Add prototype of tests --- flake.nix | 9 +- lib.nix | 186 +++++++++++++++++++++++++++++++++++ modules/cardano/cli.nix | 15 +++ modules/cardano/cli.test.nix | 22 +++++ modules/cardano/default.nix | 5 + modules/default.nix | 2 + modules/global/default.nix | 2 +- modules/testing.nix | 147 +++++++++++++++++++++++++++ 8 files changed, 386 insertions(+), 2 deletions(-) create mode 100644 lib.nix create mode 100644 modules/cardano/cli.nix create mode 100644 modules/cardano/cli.test.nix create mode 100644 modules/cardano/default.nix create mode 100644 modules/testing.nix diff --git a/flake.nix b/flake.nix index 4ca16fe..8ca486b 100644 --- a/flake.nix +++ b/flake.nix @@ -21,9 +21,16 @@ flake = false; }; }; - outputs = inputs @ {flake-parts, ...}: + outputs = inputs @ { + flake-parts, + nixpkgs, + ... + }: let + lib = nixpkgs.lib.extend (final: _: import ./lib.nix final); + in flake-parts.lib.mkFlake { inherit inputs; + specialArgs = {inherit lib;}; } { imports = [ diff --git a/lib.nix b/lib.nix new file mode 100644 index 0000000..50f4b14 --- /dev/null +++ b/lib.nix @@ -0,0 +1,186 @@ +lib: rec { + /* + Function: platformPkgs + Synopsis: Filters Nix packages based on the target system platform. + + Parameters: + - system (string): Target system platform (e.g., "x86_64-linux"). + + Returns: + - A filtered attribute set of Nix packages compatible with the target system. + */ + platformPkgs = system: + with lib; + filterAttrs + (_: value: let + platforms = attrByPath ["meta" "platforms"] [] value; + in + elem system platforms); + + /* + Function: buildApps + Synopsis: Constructs attribute set of applications from Nix packages and custom apps specification. + + Parameters: + - packages (attrset): An attribute set of Nix packages. + - apps (attrset): Custom apps specification. + + Returns: + - An attribute set representing built applications. + */ + buildApps = packages: apps: + with lib; + listToAttrs + (collect (attrs: builtins.attrNames attrs == ["name" "value"]) + (mapAttrsRecursiveCond builtins.isAttrs (path: v: let + drvName = head path; + drv = packages.${drvName}; + name = last (init path); + exePath = "/bin/${v}"; + in + nameValuePair name {inherit drv name exePath;}) + apps)); + + /* + Function: platformApps + Synopsis: Filters and builds platform-specific applications. + + Parameters: + - packages (attrset): An attribute set of Nix packages. + - apps (attrset): Custom apps specification. + + Returns: + - An attribute set of platform-specific applications. + */ + platformApps = packages: apps: + with lib; let + apps' = filterAttrs (name: _: elem name (attrNames packages)) apps; + bapps = buildApps packages apps'; + in + mapAttrs (_: mkApp) bapps; + + /* + Function: mkApp + Synopsis: Creates an "app" type for Nix flakes. + + Parameters: + - drv (derivation): The Nix derivation. + - name (string, optional): Name of the application. + - exePath (string, optional): Executable path. + + Returns: + - An "app" type attribute with 'type' and 'program' keys. + */ + mkApp = { + drv, + name ? drv.pname or drv.name, + exePath ? drv.passthru.exePath or "/bin/${name}", + }: { + type = "app"; + program = "${drv}${exePath}"; + }; + + /* + Function: flattenTree + Synopsis: Flattens a nested attribute set (tree) into a single-level attribute set. + + Parameters: + - tree (attrset): A nested attribute set + + Returns: + - An attribute set where keys are constructed in reverse DNS notation, based on the nesting. + + Example: + Input: { a = { b = { c = ; }; }; } + Output: { "a.b.c" = ; } + + Description: + The function traverses the nested attribute set and produces a flattened attribute set. + It uses dot-based reverse DNS notation to concatenate the nested keys. + */ + flattenTree = { + tree, + separator ? ".", + }: let + op = sum: path: val: let + pathStr = builtins.concatStringsSep separator path; + in + if builtins.isPath val + then + (sum + // { + "${pathStr}" = val; + }) + else if builtins.isAttrs val + then + # recurse into that attribute set + (recurse sum path val) + else + # ignore that value + sum; + + recurse = sum: path: val: + builtins.foldl' + (sum: key: op sum (path ++ [key]) val.${key}) + sum + (builtins.attrNames val); + in + recurse {} [] tree; + + /* + Function: rakeLeaves + Synopsis: Recursively collects `.nix` files from a directory into an attribute set. + + Parameters: + - dirPath (string): The directory path to collect `.nix` files from. + + Returns: + - An attribute set mapping filenames (without the `.nix` suffix) to their paths. + */ + rakeLeaves = dirPath: let + collect = file: type: { + name = lib.removeSuffix ".nix" file; + value = let + path = dirPath + "/${file}"; + in + if (type == "regular") + then path + else rakeLeaves path; + }; + + files = builtins.readDir dirPath; + in + lib.filterAttrs (_n: v: v != {}) (lib.mapAttrs' collect files); + + /* + Function: mkNixpkgs + Synopsis: Creates a custom Nixpkgs configuration. + + Parameters: + - system (string): Target system, e.g., "x86_64-linux". + - inputs (attrset, optional): Custom inputs for the Nixpkgs configuration. + - overlays (list, optional): List of overlays to apply. + - nixpkgs (path, optional): Path to the Nixpkgs repository. Defaults to inputs.nixpkgs. + - config (attrset, optional): Additional Nixpkgs configuration settings. + + Returns: + - A configured Nixpkgs environment suitable for importing. + + Example: + mkNixpkgs { + system = "x86_64-linux"; + overlays = [ myOverlay ]; + } + + Description: + The function imports a Nixpkgs environment with the specified target system, custom inputs, + and overlays. It also accepts additional Nixpkgs configuration settings. + */ + mkNixpkgs = { + system, + nixpkgs, + overlays ? [], + config ? {allowUnfree = true;}, + }: + import nixpkgs {inherit system config overlays;}; +} diff --git a/modules/cardano/cli.nix b/modules/cardano/cli.nix new file mode 100644 index 0000000..c4f1dc8 --- /dev/null +++ b/modules/cardano/cli.nix @@ -0,0 +1,15 @@ +{ + config, + lib, + pkgs, + ... +}: +with lib; { + options.cardano-ecosystem.cli.enable = mkEnableOption "Install cardano CLI tools and scripts"; + + config = mkIf config.cardano-ecosystem.cli.enable { + environment.systemPackages = with pkgs; [ + git # FIXME: we use git at scaffolding to avoid long CI builds on early prototyping stage + ]; + }; +} diff --git a/modules/cardano/cli.test.nix b/modules/cardano/cli.test.nix new file mode 100644 index 0000000..7921ac9 --- /dev/null +++ b/modules/cardano/cli.test.nix @@ -0,0 +1,22 @@ +{ + systems = ["x86_64-linux"]; + + module = _: { + name = "cli-test"; + + nodes = { + machine = { + virtualisation = { + cores = 2; + memorySize = 1024; + writableStore = true; + }; + cardano-ecosystem.cli.enable = true; + }; + }; + testScript = '' + # FIXME: check for cardano cli, not git + machine.succeed("git --version") + ''; + }; +} diff --git a/modules/cardano/default.nix b/modules/cardano/default.nix new file mode 100644 index 0000000..200e1cd --- /dev/null +++ b/modules/cardano/default.nix @@ -0,0 +1,5 @@ +{ + imports = [ + ./cli.nix + ]; +} diff --git a/modules/default.nix b/modules/default.nix index fa5ac4d..9e6adac 100644 --- a/modules/default.nix +++ b/modules/default.nix @@ -1,11 +1,13 @@ { imports = [ + ./testing.nix ]; # create a default nixos module which mixes in all modules flake.nixosModules.default = { imports = [ ./global + ./cardano ]; }; } diff --git a/modules/global/default.nix b/modules/global/default.nix index 2511ab0..1acff36 100644 --- a/modules/global/default.nix +++ b/modules/global/default.nix @@ -13,7 +13,7 @@ }; in # FIXME: proper assertion, private also can have any unused number - assert networkNumbers ? cfg.network; + # assert networkNumbers ? cfg.network; with lib; { options.cardano-ecosystem.globals = { network = mkOption { diff --git a/modules/testing.nix b/modules/testing.nix new file mode 100644 index 0000000..44d31ce --- /dev/null +++ b/modules/testing.nix @@ -0,0 +1,147 @@ +{ + self, + inputs, + ... +}: { + perSystem = { + lib, + config, + system, + ... + }: let + # create a custom nixpkgs with our flake packages available + pkgs = import inputs.nixpkgs { + inherit system; + overlays = [ + self.overlays.default + ]; + }; + in { + ######################################## + ## Interface + ######################################## + options.testing.checks = with lib; + mkOption { + type = types.attrsOf types.package; + default = {}; + }; + + ######################################## + ## Implementation + ######################################## + config.testing.checks = with lib; let + # import the testing framework + nixos-lib = import (pkgs.path + "/nixos/lib") {}; + + # traverse the filesystem and capture any files with `.test.nix` suffix + eachTest = + filterAttrs + (_: (hasSuffix ".test.nix")) + (flattenTree { + tree = rakeLeaves ./.; + separator = "-"; + }); + + # examine the `systems` attribute of each test, filtering out any that do not support the current system + eachTestForSystem = with lib; + filterAttrs + (_: v: elem system v.systems) + (mapAttrs (_: import) eachTest); + in + mapAttrs' + (name: test: + nameValuePair "testing-${removeSuffix ".test" name}" + (nixos-lib.runTest { + hostPkgs = pkgs; + + # speed up evaluation by skipping docs + defaults.documentation.enable = lib.mkDefault false; + + # make self available in test modules and our custom pkgs + node.specialArgs = {inherit self pkgs;}; + + # import all of our flake nixos modules by default + defaults.imports = [ + self.nixosModules.default + ]; + + # import the test module + imports = [test.module]; + }) + .config + .result) + eachTestForSystem; + + ######################################## + ## Commands + ######################################## + config.devshells.default.commands = [ + { + name = "tests"; + category = "Testing"; + help = "Build and run a test"; + command = with lib; '' + Help() { + # Display Help + echo " Build and run a test" + echo + echo " Usage:" + echo " test " + echo " test --interactive" + echo " test -s " + echo + echo " Arguments:" + echo " If a test package is called 'testing-nethermind-basic' then should be 'nethermind-basic'." + echo + echo " Options:" + echo " -h --help Show this screen." + echo " -l --list Show available tests." + echo " -s --system Specify the target platform [default: x84_64-linux]." + echo " -i --interactive Run the test interactively." + echo + } + + List() { + # Display available tests + echo " List of available tests:" + echo + echo "${strings.concatMapStrings (s: " - " + s + "\n") (attrsets.mapAttrsToList (name: _: (removePrefix "testing-" name)) config.testing.checks)}" + } + + ARGS=$(getopt -o lihs: --long list,interactive,help,system: -n 'tests' -- "$@") + eval set -- "$ARGS" + + SYSTEM="x86_64-linux" + DRIVER_ARGS=() + + while [ $# -gt 0 ]; do + case "$1" in + -i | --interactive) DRIVER_ARGS+=("--interactive"); shift;; + -s | --system) SYSTEM="$2"; shift 2;; + -h | --help) Help; exit 0;; + -l | --list) List; exit 0;; + -- ) shift; break;; + * ) break;; + esac + done + + if [ $# -eq 0 ]; then + # No test name has been provided + Help + exit 1 + fi + + NAME="$1" + shift + + # build the test driver + DRIVER=$(nix build ".#checks.$SYSTEM.testing-$NAME.driver" --print-out-paths --no-link) + + # run the test driver, passing any remaining arguments + set -x + ''${DRIVER}/bin/nixos-test-driver "''${DRIVER_ARGS[@]}" + ''; + } + ]; + }; +} From 0f3b151f3bb3a86beb7c94bcd42e4c171f786dc2 Mon Sep 17 00:00:00 2001 From: "Alexander V. Nikolaev" Date: Thu, 26 Oct 2023 18:10:04 +0300 Subject: [PATCH 04/21] fix: Post review fixups Signed-off-by: Alexander V. Nikolaev --- .envrc | 3 ++- flake.nix | 11 ++--------- lib/default.nix | 5 +++++ lib.nix => lib/functions.nix | 0 modules/global/default.nix | 20 ++++++++++++-------- modules/testing.nix | 4 ++-- shell/default.nix | 3 --- statix.toml | 3 --- 8 files changed, 23 insertions(+), 26 deletions(-) create mode 100644 lib/default.nix rename lib.nix => lib/functions.nix (100%) delete mode 100644 statix.toml diff --git a/.envrc b/.envrc index 6b2c0d7..21c3fa8 100644 --- a/.envrc +++ b/.envrc @@ -2,4 +2,5 @@ if ! has nix_direnv_version || ! nix_direnv_version 2.2.0; then source_url "https://raw.githubusercontent.com/nix-community/nix-direnv/2.2.0/direnvrc" "sha256-5EwyKnkJNQeXrRkYbwwRBcXbibosCJqyIUuz9Xq+LRc=" fi nix_direnv_watch_file ./shell/default.nix -use flake +nix_direnv_watch_file flake.lock +use flake -L diff --git a/flake.nix b/flake.nix index 8ca486b..3955bd0 100644 --- a/flake.nix +++ b/flake.nix @@ -21,24 +21,17 @@ flake = false; }; }; - outputs = inputs @ { - flake-parts, - nixpkgs, - ... - }: let - lib = nixpkgs.lib.extend (final: _: import ./lib.nix final); - in + outputs = inputs @ {flake-parts, ...}: flake-parts.lib.mkFlake { inherit inputs; - specialArgs = {inherit lib;}; } { imports = [ inputs.devshell.flakeModule - inputs.flake-parts.flakeModules.easyOverlay inputs.flake-root.flakeModule inputs.hercules-ci-effects.flakeModule inputs.treefmt-nix.flakeModule + ./lib ./checks ./ci ./formatter diff --git a/lib/default.nix b/lib/default.nix new file mode 100644 index 0000000..08c1786 --- /dev/null +++ b/lib/default.nix @@ -0,0 +1,5 @@ +{inputs, ...}: let + lib = import ./functions.nix inputs.nixpkgs.lib; +in { + flake.lib = lib; +} diff --git a/lib.nix b/lib/functions.nix similarity index 100% rename from lib.nix rename to lib/functions.nix diff --git a/modules/global/default.nix b/modules/global/default.nix index 1acff36..d6d2dbd 100644 --- a/modules/global/default.nix +++ b/modules/global/default.nix @@ -4,16 +4,9 @@ ... }: let cfg = config.cardano-ecosystem.globals; - # FIXME: fill other nets - # FIXME: could we extract networkNumbers automatically? - # FIXME: could we extract it from IOG nix stuff w/o IFD or massive inclusion of https://github.com/input-output-hk/iohk-nix - networkNumbers = { - mainnet = 0; - private = 42; - }; in # FIXME: proper assertion, private also can have any unused number - # assert networkNumbers ? cfg.network; + # assert cfg.networkNumbers ? cfg.network; with lib; { options.cardano-ecosystem.globals = { network = mkOption { @@ -30,5 +23,16 @@ in Cardano network number to join/use (should match cardano-ecosystem.globals,network) ''; }; + networkNumbers = mkOption { + type = types.attrsOf types.int; + # FIXME: fill other nets + # FIXME: could we extract networkNumbers automatically? + # FIXME: could we extract it from IOG nix stuff w/o IFD or massive inclusion of https://github.com/input-output-hk/iohk-nix + default = { + mainnet = 0; + private = 42; + }; + internal = true; + }; }; } diff --git a/modules/testing.nix b/modules/testing.nix index 44d31ce..1f482fd 100644 --- a/modules/testing.nix +++ b/modules/testing.nix @@ -2,7 +2,8 @@ self, inputs, ... -}: { +}: +with self.lib; { perSystem = { lib, config, @@ -13,7 +14,6 @@ pkgs = import inputs.nixpkgs { inherit system; overlays = [ - self.overlays.default ]; }; in { diff --git a/shell/default.nix b/shell/default.nix index 0cea60a..2e6bcd8 100644 --- a/shell/default.nix +++ b/shell/default.nix @@ -3,10 +3,7 @@ devshells.default = { name = "cardano.nix"; packages = with pkgs; [ - nix-update statix - mkdocs - pkgs.python310Packages.mkdocs-material ]; }; }; diff --git a/statix.toml b/statix.toml deleted file mode 100644 index 7df5742..0000000 --- a/statix.toml +++ /dev/null @@ -1,3 +0,0 @@ -disabled = [ - ".direnv" -] From 78f40047c7598bd9de974fd015609a2557cac2fd Mon Sep 17 00:00:00 2001 From: "Alexander V. Nikolaev" Date: Fri, 27 Oct 2023 20:22:29 +0300 Subject: [PATCH 05/21] feat!: remove tests --- checks/default.nix | 39 ++++------ modules/cardano/cli.test.nix | 22 ------ modules/default.nix | 1 - modules/testing.nix | 147 ----------------------------------- 4 files changed, 16 insertions(+), 193 deletions(-) delete mode 100644 modules/cardano/cli.test.nix delete mode 100644 modules/testing.nix diff --git a/checks/default.nix b/checks/default.nix index 0bb3895..15c5c6b 100644 --- a/checks/default.nix +++ b/checks/default.nix @@ -1,30 +1,23 @@ {inputs, ...}: { - perSystem = { - pkgs, - config, - ... - }: { + perSystem = {pkgs, ...}: { checks = let devour-flake = pkgs.callPackage inputs.devour-flake {}; - in - { - nix-build-all = pkgs.writeShellApplication { - name = "nix-build-all"; - runtimeInputs = [ - pkgs.nix - devour-flake - ]; - text = '' - # Make sure that flake.lock is sync - nix flake lock --no-update-lock-file + in { + nix-build-all = pkgs.writeShellApplication { + name = "nix-build-all"; + runtimeInputs = [ + pkgs.nix + devour-flake + ]; + text = '' + # Make sure that flake.lock is sync + nix flake lock --no-update-lock-file - # Do a full nix build (all outputs) - devour-flake . "$@" - ''; - }; - } - # mix in tests - // config.testing.checks; + # Do a full nix build (all outputs) + devour-flake . "$@" + ''; + }; + }; devshells.default.commands = [ { diff --git a/modules/cardano/cli.test.nix b/modules/cardano/cli.test.nix deleted file mode 100644 index 7921ac9..0000000 --- a/modules/cardano/cli.test.nix +++ /dev/null @@ -1,22 +0,0 @@ -{ - systems = ["x86_64-linux"]; - - module = _: { - name = "cli-test"; - - nodes = { - machine = { - virtualisation = { - cores = 2; - memorySize = 1024; - writableStore = true; - }; - cardano-ecosystem.cli.enable = true; - }; - }; - testScript = '' - # FIXME: check for cardano cli, not git - machine.succeed("git --version") - ''; - }; -} diff --git a/modules/default.nix b/modules/default.nix index 9e6adac..52c477c 100644 --- a/modules/default.nix +++ b/modules/default.nix @@ -1,6 +1,5 @@ { imports = [ - ./testing.nix ]; # create a default nixos module which mixes in all modules diff --git a/modules/testing.nix b/modules/testing.nix deleted file mode 100644 index 1f482fd..0000000 --- a/modules/testing.nix +++ /dev/null @@ -1,147 +0,0 @@ -{ - self, - inputs, - ... -}: -with self.lib; { - perSystem = { - lib, - config, - system, - ... - }: let - # create a custom nixpkgs with our flake packages available - pkgs = import inputs.nixpkgs { - inherit system; - overlays = [ - ]; - }; - in { - ######################################## - ## Interface - ######################################## - options.testing.checks = with lib; - mkOption { - type = types.attrsOf types.package; - default = {}; - }; - - ######################################## - ## Implementation - ######################################## - config.testing.checks = with lib; let - # import the testing framework - nixos-lib = import (pkgs.path + "/nixos/lib") {}; - - # traverse the filesystem and capture any files with `.test.nix` suffix - eachTest = - filterAttrs - (_: (hasSuffix ".test.nix")) - (flattenTree { - tree = rakeLeaves ./.; - separator = "-"; - }); - - # examine the `systems` attribute of each test, filtering out any that do not support the current system - eachTestForSystem = with lib; - filterAttrs - (_: v: elem system v.systems) - (mapAttrs (_: import) eachTest); - in - mapAttrs' - (name: test: - nameValuePair "testing-${removeSuffix ".test" name}" - (nixos-lib.runTest { - hostPkgs = pkgs; - - # speed up evaluation by skipping docs - defaults.documentation.enable = lib.mkDefault false; - - # make self available in test modules and our custom pkgs - node.specialArgs = {inherit self pkgs;}; - - # import all of our flake nixos modules by default - defaults.imports = [ - self.nixosModules.default - ]; - - # import the test module - imports = [test.module]; - }) - .config - .result) - eachTestForSystem; - - ######################################## - ## Commands - ######################################## - config.devshells.default.commands = [ - { - name = "tests"; - category = "Testing"; - help = "Build and run a test"; - command = with lib; '' - Help() { - # Display Help - echo " Build and run a test" - echo - echo " Usage:" - echo " test " - echo " test --interactive" - echo " test -s " - echo - echo " Arguments:" - echo " If a test package is called 'testing-nethermind-basic' then should be 'nethermind-basic'." - echo - echo " Options:" - echo " -h --help Show this screen." - echo " -l --list Show available tests." - echo " -s --system Specify the target platform [default: x84_64-linux]." - echo " -i --interactive Run the test interactively." - echo - } - - List() { - # Display available tests - echo " List of available tests:" - echo - echo "${strings.concatMapStrings (s: " - " + s + "\n") (attrsets.mapAttrsToList (name: _: (removePrefix "testing-" name)) config.testing.checks)}" - } - - ARGS=$(getopt -o lihs: --long list,interactive,help,system: -n 'tests' -- "$@") - eval set -- "$ARGS" - - SYSTEM="x86_64-linux" - DRIVER_ARGS=() - - while [ $# -gt 0 ]; do - case "$1" in - -i | --interactive) DRIVER_ARGS+=("--interactive"); shift;; - -s | --system) SYSTEM="$2"; shift 2;; - -h | --help) Help; exit 0;; - -l | --list) List; exit 0;; - -- ) shift; break;; - * ) break;; - esac - done - - if [ $# -eq 0 ]; then - # No test name has been provided - Help - exit 1 - fi - - NAME="$1" - shift - - # build the test driver - DRIVER=$(nix build ".#checks.$SYSTEM.testing-$NAME.driver" --print-out-paths --no-link) - - # run the test driver, passing any remaining arguments - set -x - ''${DRIVER}/bin/nixos-test-driver "''${DRIVER_ARGS[@]}" - ''; - } - ]; - }; -} From a2560699d828d1a1e8610163ff5fe63f5e5e144c Mon Sep 17 00:00:00 2001 From: "Alexander V. Nikolaev" Date: Fri, 27 Oct 2023 20:35:26 +0300 Subject: [PATCH 06/21] feat(shell): treefmt binary in shell --- shell/default.nix | 1 + 1 file changed, 1 insertion(+) diff --git a/shell/default.nix b/shell/default.nix index 2e6bcd8..9cf9172 100644 --- a/shell/default.nix +++ b/shell/default.nix @@ -4,6 +4,7 @@ name = "cardano.nix"; packages = with pkgs; [ statix + config.treefmt.build.wrapper ]; }; }; From 24614f1fcb6eecfa6465fee0a92ead90f04de843 Mon Sep 17 00:00:00 2001 From: "Alexander V. Nikolaev" Date: Sat, 28 Oct 2023 14:19:52 +0300 Subject: [PATCH 07/21] chore: reorganize modules --- modules/default.nix | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/modules/default.nix b/modules/default.nix index 52c477c..2cabb66 100644 --- a/modules/default.nix +++ b/modules/default.nix @@ -3,10 +3,11 @@ ]; # create a default nixos module which mixes in all modules - flake.nixosModules.default = { - imports = [ - ./global - ./cardano - ]; + flake.nixosModules = { + global = ./global; + cardano-cli = ./cardano/cli.nix; + default = { + imports = with builtins; attrValues (removeAttrs config.flake.nixosModules ["default"]); + }; }; } From 2aaeb6782e20830c983e76acd05704baa1caa38f Mon Sep 17 00:00:00 2001 From: "Alexander V. Nikolaev" Date: Sat, 28 Oct 2023 16:44:27 +0300 Subject: [PATCH 08/21] feat!: unstage lib --- lib/functions.nix | 186 +--------------------------------------------- 1 file changed, 1 insertion(+), 185 deletions(-) diff --git a/lib/functions.nix b/lib/functions.nix index 50f4b14..34dca26 100644 --- a/lib/functions.nix +++ b/lib/functions.nix @@ -1,186 +1,2 @@ -lib: rec { - /* - Function: platformPkgs - Synopsis: Filters Nix packages based on the target system platform. - - Parameters: - - system (string): Target system platform (e.g., "x86_64-linux"). - - Returns: - - A filtered attribute set of Nix packages compatible with the target system. - */ - platformPkgs = system: - with lib; - filterAttrs - (_: value: let - platforms = attrByPath ["meta" "platforms"] [] value; - in - elem system platforms); - - /* - Function: buildApps - Synopsis: Constructs attribute set of applications from Nix packages and custom apps specification. - - Parameters: - - packages (attrset): An attribute set of Nix packages. - - apps (attrset): Custom apps specification. - - Returns: - - An attribute set representing built applications. - */ - buildApps = packages: apps: - with lib; - listToAttrs - (collect (attrs: builtins.attrNames attrs == ["name" "value"]) - (mapAttrsRecursiveCond builtins.isAttrs (path: v: let - drvName = head path; - drv = packages.${drvName}; - name = last (init path); - exePath = "/bin/${v}"; - in - nameValuePair name {inherit drv name exePath;}) - apps)); - - /* - Function: platformApps - Synopsis: Filters and builds platform-specific applications. - - Parameters: - - packages (attrset): An attribute set of Nix packages. - - apps (attrset): Custom apps specification. - - Returns: - - An attribute set of platform-specific applications. - */ - platformApps = packages: apps: - with lib; let - apps' = filterAttrs (name: _: elem name (attrNames packages)) apps; - bapps = buildApps packages apps'; - in - mapAttrs (_: mkApp) bapps; - - /* - Function: mkApp - Synopsis: Creates an "app" type for Nix flakes. - - Parameters: - - drv (derivation): The Nix derivation. - - name (string, optional): Name of the application. - - exePath (string, optional): Executable path. - - Returns: - - An "app" type attribute with 'type' and 'program' keys. - */ - mkApp = { - drv, - name ? drv.pname or drv.name, - exePath ? drv.passthru.exePath or "/bin/${name}", - }: { - type = "app"; - program = "${drv}${exePath}"; - }; - - /* - Function: flattenTree - Synopsis: Flattens a nested attribute set (tree) into a single-level attribute set. - - Parameters: - - tree (attrset): A nested attribute set - - Returns: - - An attribute set where keys are constructed in reverse DNS notation, based on the nesting. - - Example: - Input: { a = { b = { c = ; }; }; } - Output: { "a.b.c" = ; } - - Description: - The function traverses the nested attribute set and produces a flattened attribute set. - It uses dot-based reverse DNS notation to concatenate the nested keys. - */ - flattenTree = { - tree, - separator ? ".", - }: let - op = sum: path: val: let - pathStr = builtins.concatStringsSep separator path; - in - if builtins.isPath val - then - (sum - // { - "${pathStr}" = val; - }) - else if builtins.isAttrs val - then - # recurse into that attribute set - (recurse sum path val) - else - # ignore that value - sum; - - recurse = sum: path: val: - builtins.foldl' - (sum: key: op sum (path ++ [key]) val.${key}) - sum - (builtins.attrNames val); - in - recurse {} [] tree; - - /* - Function: rakeLeaves - Synopsis: Recursively collects `.nix` files from a directory into an attribute set. - - Parameters: - - dirPath (string): The directory path to collect `.nix` files from. - - Returns: - - An attribute set mapping filenames (without the `.nix` suffix) to their paths. - */ - rakeLeaves = dirPath: let - collect = file: type: { - name = lib.removeSuffix ".nix" file; - value = let - path = dirPath + "/${file}"; - in - if (type == "regular") - then path - else rakeLeaves path; - }; - - files = builtins.readDir dirPath; - in - lib.filterAttrs (_n: v: v != {}) (lib.mapAttrs' collect files); - - /* - Function: mkNixpkgs - Synopsis: Creates a custom Nixpkgs configuration. - - Parameters: - - system (string): Target system, e.g., "x86_64-linux". - - inputs (attrset, optional): Custom inputs for the Nixpkgs configuration. - - overlays (list, optional): List of overlays to apply. - - nixpkgs (path, optional): Path to the Nixpkgs repository. Defaults to inputs.nixpkgs. - - config (attrset, optional): Additional Nixpkgs configuration settings. - - Returns: - - A configured Nixpkgs environment suitable for importing. - - Example: - mkNixpkgs { - system = "x86_64-linux"; - overlays = [ myOverlay ]; - } - - Description: - The function imports a Nixpkgs environment with the specified target system, custom inputs, - and overlays. It also accepts additional Nixpkgs configuration settings. - */ - mkNixpkgs = { - system, - nixpkgs, - overlays ? [], - config ? {allowUnfree = true;}, - }: - import nixpkgs {inherit system config overlays;}; +_lib: rec { } From d1f01ab0d7270cea935553bad59c506fb58601e7 Mon Sep 17 00:00:00 2001 From: "Alexander V. Nikolaev" Date: Sat, 28 Oct 2023 16:52:42 +0300 Subject: [PATCH 09/21] fix: Post-review fixups --- .envrc | 3 +-- lib/default.nix | 6 +++--- modules/global/default.nix | 3 +++ 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/.envrc b/.envrc index 21c3fa8..4898c86 100644 --- a/.envrc +++ b/.envrc @@ -1,6 +1,5 @@ if ! has nix_direnv_version || ! nix_direnv_version 2.2.0; then source_url "https://raw.githubusercontent.com/nix-community/nix-direnv/2.2.0/direnvrc" "sha256-5EwyKnkJNQeXrRkYbwwRBcXbibosCJqyIUuz9Xq+LRc=" fi -nix_direnv_watch_file ./shell/default.nix -nix_direnv_watch_file flake.lock +nix_direnv_watch_file ./shell/default.nix flake.lock use flake -L diff --git a/lib/default.nix b/lib/default.nix index 08c1786..c8d977e 100644 --- a/lib/default.nix +++ b/lib/default.nix @@ -1,5 +1,5 @@ -{inputs, ...}: let - lib = import ./functions.nix inputs.nixpkgs.lib; +{lib, ...}: let + lib' = import ./functions.nix lib; in { - flake.lib = lib; + flake.lib = lib'; } diff --git a/modules/global/default.nix b/modules/global/default.nix index d6d2dbd..7aed610 100644 --- a/modules/global/default.nix +++ b/modules/global/default.nix @@ -30,6 +30,9 @@ in # FIXME: could we extract it from IOG nix stuff w/o IFD or massive inclusion of https://github.com/input-output-hk/iohk-nix default = { mainnet = 0; + preprod = 1; + preview = 2; + sanchonet = 4; private = 42; }; internal = true; From 344cdad33b0970d7617e9c845d83354dc6c5bd2c Mon Sep 17 00:00:00 2001 From: Andrea Ciceri Date: Mon, 30 Oct 2023 18:01:28 +0100 Subject: [PATCH 10/21] refactor: `nix-build-all` as flake's app and devshell command --- checks/default.nix | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/checks/default.nix b/checks/default.nix index 15c5c6b..d14f496 100644 --- a/checks/default.nix +++ b/checks/default.nix @@ -1,13 +1,14 @@ {inputs, ...}: { - perSystem = {pkgs, ...}: { - checks = let - devour-flake = pkgs.callPackage inputs.devour-flake {}; - in { - nix-build-all = pkgs.writeShellApplication { + perSystem = { + pkgs, + config, + ... + }: { + apps = { + nix-build-all.program = pkgs.writeShellApplication { name = "nix-build-all"; runtimeInputs = [ - pkgs.nix - devour-flake + (pkgs.callPackage inputs.devour-flake {}) ]; text = '' # Make sure that flake.lock is sync @@ -23,8 +24,8 @@ { category = "Tools"; name = "check"; - help = "Checks the source tree"; - command = "nix flake check"; + help = "Build all the checks"; + command = config.apps.nix-build-all.program; } ]; }; From 283d0d22f9dd05981d849fed29fbd83e8d2d43c1 Mon Sep 17 00:00:00 2001 From: Andrea Ciceri Date: Mon, 30 Oct 2023 18:05:09 +0100 Subject: [PATCH 11/21] ops: enable CI for `x86_64-darwin` --- ci/default.nix | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ci/default.nix b/ci/default.nix index b850b87..d7e7eae 100644 --- a/ci/default.nix +++ b/ci/default.nix @@ -1,3 +1,3 @@ { - config.herculesCI.ciSystems = ["x86_64-linux"]; + config.herculesCI.ciSystems = ["x86_64-linux" "x86_64-darwin"]; } From b3b18a68cbd93d4f4fa1243cbdddd4e4005bb4ee Mon Sep 17 00:00:00 2001 From: Andrea Ciceri Date: Mon, 30 Oct 2023 18:14:46 +0100 Subject: [PATCH 12/21] refactor: rename dummy module --- modules/{cardano/cli.nix => cardano-cli/default.nix} | 2 +- modules/cardano/default.nix | 5 ----- modules/default.nix | 4 ++-- 3 files changed, 3 insertions(+), 8 deletions(-) rename modules/{cardano/cli.nix => cardano-cli/default.nix} (73%) delete mode 100644 modules/cardano/default.nix diff --git a/modules/cardano/cli.nix b/modules/cardano-cli/default.nix similarity index 73% rename from modules/cardano/cli.nix rename to modules/cardano-cli/default.nix index c4f1dc8..e6c72de 100644 --- a/modules/cardano/cli.nix +++ b/modules/cardano-cli/default.nix @@ -9,7 +9,7 @@ with lib; { config = mkIf config.cardano-ecosystem.cli.enable { environment.systemPackages = with pkgs; [ - git # FIXME: we use git at scaffolding to avoid long CI builds on early prototyping stage + git # FIXME: just a placeholder ]; }; } diff --git a/modules/cardano/default.nix b/modules/cardano/default.nix deleted file mode 100644 index 200e1cd..0000000 --- a/modules/cardano/default.nix +++ /dev/null @@ -1,5 +0,0 @@ -{ - imports = [ - ./cli.nix - ]; -} diff --git a/modules/default.nix b/modules/default.nix index 2cabb66..a70814e 100644 --- a/modules/default.nix +++ b/modules/default.nix @@ -2,10 +2,10 @@ imports = [ ]; - # create a default nixos module which mixes in all modules flake.nixosModules = { global = ./global; - cardano-cli = ./cardano/cli.nix; + cardano-cli = ./cardano-cli; + # the default module imports all modules default = { imports = with builtins; attrValues (removeAttrs config.flake.nixosModules ["default"]); }; From 02d1ed0c414f8edc6e58fae808b705d59e50224b Mon Sep 17 00:00:00 2001 From: Andrea Ciceri Date: Mon, 30 Oct 2023 18:16:24 +0100 Subject: [PATCH 13/21] chore: `.direnv` in gitignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ff51edf --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.direnv \ No newline at end of file From f74210a942a874676aae1ffb9d4e28b8597c8432 Mon Sep 17 00:00:00 2001 From: Andrea Ciceri Date: Mon, 30 Oct 2023 20:54:45 +0100 Subject: [PATCH 14/21] fix: import shell module --- flake.nix | 1 + 1 file changed, 1 insertion(+) diff --git a/flake.nix b/flake.nix index 3955bd0..4d2c593 100644 --- a/flake.nix +++ b/flake.nix @@ -36,6 +36,7 @@ ./ci ./formatter ./modules + ./shell ]; systems = [ "x86_64-linux" From f622fb22d3c65c5eea238be30ceed963a5c3313d Mon Sep 17 00:00:00 2001 From: Andrea Ciceri Date: Mon, 30 Oct 2023 20:55:13 +0100 Subject: [PATCH 15/21] feat: improve shell --- shell/default.nix | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/shell/default.nix b/shell/default.nix index 9cf9172..3ec7b3c 100644 --- a/shell/default.nix +++ b/shell/default.nix @@ -1,10 +1,16 @@ { perSystem = {pkgs, ...}: { devshells.default = { - name = "cardano.nix"; + devshell = { + name = "cardano.nix"; + motd = '' + ❄️ Welcome to the {14}{bold}cardano.nix{reset}'s shell ❄️ + $(type -p menu &>/dev/null && menu) + ''; + }; packages = with pkgs; [ statix - config.treefmt.build.wrapper + # config.treefmt.build.wrapper ]; }; }; From fdeea1f8eb416ce30fe70d9464c2512d59e762f2 Mon Sep 17 00:00:00 2001 From: Andrea Ciceri Date: Mon, 30 Oct 2023 20:55:48 +0100 Subject: [PATCH 16/21] refactor: new `check` shell command and renamed old one to `build-all` --- checks/default.nix | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/checks/default.nix b/checks/default.nix index d14f496..7e198b6 100644 --- a/checks/default.nix +++ b/checks/default.nix @@ -23,10 +23,16 @@ devshells.default.commands = [ { category = "Tools"; - name = "check"; + name = "build-all"; help = "Build all the checks"; command = config.apps.nix-build-all.program; } + { + category = "Tools"; + name = "check"; + help = "Alias of `nix flake check`"; + command = "nix flake check"; + } ]; }; } From 9f983d160b88174b21beb5cc5c87178685cabb10 Mon Sep 17 00:00:00 2001 From: Andrea Ciceri Date: Mon, 30 Oct 2023 20:59:10 +0100 Subject: [PATCH 17/21] feat: initial README --- README.md | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/README.md b/README.md index e35bb0e..0e198b5 100644 --- a/README.md +++ b/README.md @@ -1 +1,17 @@ # cardano.nix + +Collection of Cardano related NixOS modules, with a special focus on: + +- autogenerated documentation +- comprehensive (NixOS) tests +- an homogeneous module interface + +## Shell + +`cardano.nix` provides a shell that includes some useful aliases: + +- `fmt` formats the entire repository using [treefmt](https://github.com/numtide/treefmt) +- `build-all` builds all the flake's outputs using [devour-flake](https://github.com/srid/devour-flake) +- `check` simply stands for `nix flake check` + +A `.envrc` is also provided, using [direnv]() and [nix-direnv](https://github.com/nix-community/nix-direnv) is highly suggested. From eb1f7e9a0ca9939169ca6f79bc689ac7699e6898 Mon Sep 17 00:00:00 2001 From: Andrea Ciceri Date: Mon, 30 Oct 2023 21:00:03 +0100 Subject: [PATCH 18/21] feat(shell): add `treefmt` binary --- shell/default.nix | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/shell/default.nix b/shell/default.nix index 3ec7b3c..327aab2 100644 --- a/shell/default.nix +++ b/shell/default.nix @@ -1,5 +1,9 @@ { - perSystem = {pkgs, ...}: { + perSystem = { + pkgs, + config, + ... + }: { devshells.default = { devshell = { name = "cardano.nix"; @@ -10,7 +14,7 @@ }; packages = with pkgs; [ statix - # config.treefmt.build.wrapper + config.treefmt.build.wrapper ]; }; }; From cdc7c239d543d86bd9d9771eb87c701d87b3e194 Mon Sep 17 00:00:00 2001 From: Andrea Ciceri Date: Mon, 30 Oct 2023 21:25:31 +0100 Subject: [PATCH 19/21] refactor: reorganize imports --- ci/default.nix | 5 ++++- flake.nix | 4 ---- formatter/default.nix | 8 +++++++- shell/default.nix | 7 ++++++- 4 files changed, 17 insertions(+), 7 deletions(-) diff --git a/ci/default.nix b/ci/default.nix index d7e7eae..43be2f5 100644 --- a/ci/default.nix +++ b/ci/default.nix @@ -1,3 +1,6 @@ -{ +{inputs, ...}: { + imports = [ + inputs.hercules-ci-effects.flakeModule + ]; config.herculesCI.ciSystems = ["x86_64-linux" "x86_64-darwin"]; } diff --git a/flake.nix b/flake.nix index 4d2c593..05cb35c 100644 --- a/flake.nix +++ b/flake.nix @@ -27,10 +27,6 @@ } { imports = [ - inputs.devshell.flakeModule - inputs.flake-root.flakeModule - inputs.hercules-ci-effects.flakeModule - inputs.treefmt-nix.flakeModule ./lib ./checks ./ci diff --git a/formatter/default.nix b/formatter/default.nix index 809cf71..ab0086a 100644 --- a/formatter/default.nix +++ b/formatter/default.nix @@ -1,7 +1,13 @@ -{ +{inputs, ...}: { + imports = [ + inputs.flake-root.flakeModule + inputs.treefmt-nix.flakeModule + ]; + perSystem = { config, pkgs, + lib, ... }: { treefmt.config = { diff --git a/shell/default.nix b/shell/default.nix index 327aab2..5a8f7c1 100644 --- a/shell/default.nix +++ b/shell/default.nix @@ -1,4 +1,8 @@ -{ +{inputs, ...}: { + imports = [ + inputs.devshell.flakeModule + ]; + perSystem = { pkgs, config, @@ -16,6 +20,7 @@ statix config.treefmt.build.wrapper ]; + }; }; } From 942c6eadd5edb6de2d3dc2b11d4fb8a56e181dbc Mon Sep 17 00:00:00 2001 From: Andrea Ciceri Date: Mon, 30 Oct 2023 21:31:32 +0100 Subject: [PATCH 20/21] feat(shell): directly use treefmt path --- formatter/default.nix | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/formatter/default.nix b/formatter/default.nix index ab0086a..c3aef65 100644 --- a/formatter/default.nix +++ b/formatter/default.nix @@ -28,7 +28,7 @@ category = "Tools"; name = "fmt"; help = "Format the source tree"; - command = "nix fmt"; + command = lib.getExe config.treefmt.build.wrapper; } ]; }; From fd4fa6d58d4daeb6bcacae01b32145e4367bc7c8 Mon Sep 17 00:00:00 2001 From: Andrea Ciceri Date: Mon, 30 Oct 2023 21:49:11 +0100 Subject: [PATCH 21/21] style: format --- shell/default.nix | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/shell/default.nix b/shell/default.nix index 5a8f7c1..d9584e9 100644 --- a/shell/default.nix +++ b/shell/default.nix @@ -2,7 +2,7 @@ imports = [ inputs.devshell.flakeModule ]; - + perSystem = { pkgs, config, @@ -20,7 +20,6 @@ statix config.treefmt.build.wrapper ]; - }; }; }