diff --git a/.envrc b/.envrc new file mode 100644 index 0000000..4898c86 --- /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 flake.lock +use flake -L 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 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. diff --git a/checks/default.nix b/checks/default.nix new file mode 100644 index 0000000..7e198b6 --- /dev/null +++ b/checks/default.nix @@ -0,0 +1,38 @@ +{inputs, ...}: { + perSystem = { + pkgs, + config, + ... + }: { + apps = { + nix-build-all.program = pkgs.writeShellApplication { + name = "nix-build-all"; + runtimeInputs = [ + (pkgs.callPackage inputs.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 . "$@" + ''; + }; + }; + + devshells.default.commands = [ + { + category = "Tools"; + 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"; + } + ]; + }; +} diff --git a/ci/default.nix b/ci/default.nix new file mode 100644 index 0000000..43be2f5 --- /dev/null +++ b/ci/default.nix @@ -0,0 +1,6 @@ +{inputs, ...}: { + imports = [ + inputs.hercules-ci-effects.flakeModule + ]; + config.herculesCI.ciSystems = ["x86_64-linux" "x86_64-darwin"]; +} 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..05cb35c --- /dev/null +++ b/flake.nix @@ -0,0 +1,44 @@ +{ + 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 = [ + ./lib + ./checks + ./ci + ./formatter + ./modules + ./shell + ]; + 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..c3aef65 --- /dev/null +++ b/formatter/default.nix @@ -0,0 +1,35 @@ +{inputs, ...}: { + imports = [ + inputs.flake-root.flakeModule + inputs.treefmt-nix.flakeModule + ]; + + perSystem = { + config, + pkgs, + lib, + ... + }: { + 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 = lib.getExe config.treefmt.build.wrapper; + } + ]; + }; +} diff --git a/lib/default.nix b/lib/default.nix new file mode 100644 index 0000000..c8d977e --- /dev/null +++ b/lib/default.nix @@ -0,0 +1,5 @@ +{lib, ...}: let + lib' = import ./functions.nix lib; +in { + flake.lib = lib'; +} diff --git a/lib/functions.nix b/lib/functions.nix new file mode 100644 index 0000000..34dca26 --- /dev/null +++ b/lib/functions.nix @@ -0,0 +1,2 @@ +_lib: rec { +} diff --git a/modules/cardano-cli/default.nix b/modules/cardano-cli/default.nix new file mode 100644 index 0000000..e6c72de --- /dev/null +++ b/modules/cardano-cli/default.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: just a placeholder + ]; + }; +} diff --git a/modules/default.nix b/modules/default.nix new file mode 100644 index 0000000..a70814e --- /dev/null +++ b/modules/default.nix @@ -0,0 +1,13 @@ +{ + imports = [ + ]; + + flake.nixosModules = { + global = ./global; + cardano-cli = ./cardano-cli; + # the default module imports all modules + default = { + imports = with builtins; attrValues (removeAttrs config.flake.nixosModules ["default"]); + }; + }; +} diff --git a/modules/global/default.nix b/modules/global/default.nix new file mode 100644 index 0000000..7aed610 --- /dev/null +++ b/modules/global/default.nix @@ -0,0 +1,41 @@ +{ + lib, + config, + ... +}: let + cfg = config.cardano-ecosystem.globals; +in + # FIXME: proper assertion, private also can have any unused number + # assert cfg.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) + ''; + }; + 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; + preprod = 1; + preview = 2; + sanchonet = 4; + private = 42; + }; + internal = true; + }; + }; + } diff --git a/shell/default.nix b/shell/default.nix new file mode 100644 index 0000000..d9584e9 --- /dev/null +++ b/shell/default.nix @@ -0,0 +1,25 @@ +{inputs, ...}: { + imports = [ + inputs.devshell.flakeModule + ]; + + perSystem = { + pkgs, + config, + ... + }: { + devshells.default = { + 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 + ]; + }; + }; +}