diff --git a/flake.lock b/flake.lock index c89e3dc..e86af28 100644 --- a/flake.lock +++ b/flake.lock @@ -18,21 +18,6 @@ "type": "github" } }, - "nixpkgs": { - "locked": { - "lastModified": 1764983851, - "narHash": "sha256-y7RPKl/jJ/KAP/VKLMghMgXTlvNIJMHKskl8/Uuar7o=", - "owner": "NixOS", - "repo": "nixpkgs", - "rev": "d9bc5c7dceb30d8d6fafa10aeb6aa8a48c218454", - "type": "github" - }, - "original": { - "id": "nixpkgs", - "ref": "nixos-25.11", - "type": "indirect" - } - }, "nixpkgs-lib": { "locked": { "lastModified": 1761765539, @@ -48,10 +33,28 @@ "type": "github" } }, + "nixpkgs-lib_2": { + "locked": { + "dir": "lib", + "lastModified": 1765311797, + "narHash": "sha256-mSD5Ob7a+T2RNjvPvOA1dkJHGVrNVl8ZOrAwBjKBDQo=", + "owner": "Nixos", + "repo": "nixpkgs", + "rev": "09eb77e94fa25202af8f3e81ddc7353d9970ac1b", + "type": "github" + }, + "original": { + "dir": "lib", + "owner": "Nixos", + "ref": "nixos-25.11", + "repo": "nixpkgs", + "type": "github" + } + }, "root": { "inputs": { "flake-parts": "flake-parts", - "nixpkgs": "nixpkgs" + "nixpkgs-lib": "nixpkgs-lib_2" } } }, diff --git a/flake.nix b/flake.nix index 84daa95..5f97580 100644 --- a/flake.nix +++ b/flake.nix @@ -1,34 +1,21 @@ { description = "Helper for a declarative fusesoc environment"; + # See: https://github.com/VTimofeenko/writing-flake-modules/blob/master/example-1-configurable-inputs-bumper/provider/flake.nix inputs = { - nixpkgs.url = "nixpkgs/nixos-25.11"; + nixpkgs-lib.url = "github:Nixos/nixpkgs/nixos-25.11?dir=lib"; flake-parts.url = "github:hercules-ci/flake-parts"; }; outputs = inputs@{ flake-parts, ... }: - flake-parts.lib.mkFlake { inherit inputs; } { - systems = [ - "x86_64-linux" - ]; - perSystem = - { - config, - self', - inputs', - pkgs, - system, - ... - }: - { - packages = { - inherit (pkgs.callPackage ./lib.nix { }) - mkFusesocLib - mkFusesocConf - mkFusesocShell - ; - }; + flake-parts.lib.mkFlake { inherit inputs; } ( + { flake-parts-lib, ... }: + { + flake.flakeModule = flake-parts-lib.importApply ./flakeModule.nix { + inherit flake-parts-lib; + inherit (inputs) nixpkgs-lib; }; - }; + } + ); } diff --git a/flakeModule.nix b/flakeModule.nix new file mode 100644 index 0000000..2e2f450 --- /dev/null +++ b/flakeModule.nix @@ -0,0 +1,77 @@ +{ nixpkgs-lib, flake-parts-lib, ... }: +let + inherit (flake-parts-lib) mkPerSystemOption; + inherit (nixpkgs-lib) lib; +in +{ + config, + self, + inputs, + ... +}: +{ + options.perSystem = mkPerSystemOption ( + { config, pkgs, ... }: + let + cfg = config.fusesoc-project; + in + { + # TODO: multi-project with .. pattern mapping to devShell. + options.fusesoc-project = + let + inherit (lib) types; + in + { + withVerilator = lib.mkEnableOption "Include verilator"; + withCcache = lib.mkEnableOption "Use ccache with verilator"; + sources = lib.mkOption { + type = types.attrsOf (types.either types.path types.str); + default = { }; + }; + extraPackages = lib.mkOption { + type = types.listOf types.package; + default = [ ]; + }; + }; + config = + let + mkFusesocLib = name: path: '' + [library.${name}] + location = ${path} + sync-uri = ${path} + sync-type = local + auto-sync = false + ''; + mkFusesocConf = sources: builtins.concatStringsSep "\n" (lib.mapAttrsToList (mkFusesocLib) sources); + in + { + # See: https://flake.parts/options/devshell.html + devshells.default = + let + fusesocConf = pkgs.writeTextFile { + name = "fusesoc.conf"; + text = mkFusesocConf cfg.sources; + }; + fusesocWrapped = pkgs.writeShellScriptBin "fusesoc" '' + exec ${pkgs.fusesoc}/bin/fusesoc --config ${fusesocConf} $@ + ''; + in + { + packages = lib.concatLists [ + [ fusesocWrapped ] + (lib.optionals cfg.withVerilator [ + pkgs.verilator + pkgs.zlib.dev + ]) + (lib.optional cfg.withCcache pkgs.ccache) + cfg.extraPackages + ]; + env = lib.optional cfg.withCcache { + name = "OBJCACHE"; + value = "ccache"; + }; + }; + }; + } + ); +} diff --git a/lib.nix b/lib.nix deleted file mode 100644 index dbaa426..0000000 --- a/lib.nix +++ /dev/null @@ -1,55 +0,0 @@ -{ - fusesoc, - verilator, - ccache, - zlib, - mkShell, - writeTextFile, - writeShellScriptBin, - lib, - ... -}: -rec { - mkFusesocLib = name: path: '' - [library.${name}] - location = ${path} - sync-uri = ${path} - sync-type = local - auto-sync = false - ''; - - mkFusesocConf = sources: builtins.concatStringsSep "\n" (lib.mapAttrsToList (mkFusesocLib) sources); - - mkFusesocShell = - { - fusesoc ? fusesoc, - sources ? { - _localCores = "."; - }, - withVerilator ? false, - verilator ? verilator, - withCcache ? false, - ccache ? ccache, - extraPackages ? [ ], - }: - let - fusesocConf = writeTextFile "fusesoc.conf" (mkFusesocConf sources); - fusesocWrapped = writeShellScriptBin "fusesoc" '' - exec ${fusesoc}/bin/fusesoc --config ${fusesocConf} \$@ - ''; - in - mkShell { - packages = lib.concatLists [ - [ fusesocWrapped ] - (lib.optionals withVerilator [ - verilator - zlib.dev - ]) - (lib.optional withCcache ccache) - extraPackages - ]; - shellHook = '' - ${lib.optionalString withCcache "export OBJCACHE=ccache"} - ''; - }; -}