diff --git a/README.md b/README.md index f59dac9..9b46702 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,40 @@ # flake-utils -Pure Nix flake utility functions + +**STATUS: WIP** + +Pure Nix flake utility functions. + +The goal of this project is to build a collection of pure Nix functions that don't +depend on nixpkgs, and that are useful in the context of writing other Nix +flakes. + +## Usage + +`flake.nix` +```nix +{ + edition = 201909; + description = "My flake"; + inputs = { + utils = { type = "github"; owner = "numtide"; repo = "flake-utils"; }; + }; + outputs = { self, nixpkgs, utils }: + utils.eachDefaultSystem (system: + let pkgs = nixpkgs.legacyPackages.${system}; in + rec { + packages = { + my-app = pkgs.callPackage ./my-app.nix {}; + }; + + defaultPackage = package.my-app; + + apps = { + my-app = flake.mkApp packages.my-app; + }; + + defaultApp = apps.my-app; + }; + ); +} +``` + diff --git a/default.nix b/default.nix index c79dea3..122cef6 100644 --- a/default.nix +++ b/default.nix @@ -1,28 +1,41 @@ -rec { +let # copied from genAttrs = names: f: builtins.listToAttrs (map (n: { name = n; value = f n; }) names); + mapAttrsToList = f: attrs: + map (name: f name attrs.${name}) (builtins.attrNames attrs); + # The list of systems supported by nixpkgs and hydra - supportedSystems = [ + defaultSystems = [ "aarch64-linux" "i686-linux" "x86_64-darwin" "x86_64-linux" ]; - # Returns an attribute set with all the supported systems as keys and the - # output of the passed function with each system passed to it. + # eachSystem using defaultSystems + eachDefaultSystem = eachSystem defaultSystems; + + # Builds a map from =value to .=value for each system. # - # This is useful in the flake outputs because the outputs return static sets - # that map to the different systems. # - # Example: - # forAllSupported (x: null) - # > { aarch64-linux = null; i686-linux = null; x86_64-darwin = null; - # > x86_64-linux = null; } - # (system -> attrs) -> attrs - forAllSupported = genAttrs supportedSystems; + eachSystem = systems: f: + let + op = attrs: system: + let + ret = f system; + opt = attrs: key: + attrs // + { + ${key} = (attrs.${key} or {}) // { ${system} = ret.${key}; }; + } + ; + in + builtins.foldl' op attrs (builtins.attrNames ret); + in + builtins.foldl' op {} systems + ; # Returns the structure used by `nix app` mkApp = @@ -34,4 +47,12 @@ rec { type = "app"; program = "${drv}${exePath}"; }; +in +{ + inherit + defaultSystems + eachDefaultSystem + eachSystem + mkApp + ; } diff --git a/example/flake.nix b/example/flake.nix new file mode 100644 index 0000000..d2150e5 --- /dev/null +++ b/example/flake.nix @@ -0,0 +1,20 @@ +{ + description = "Flake utils demo"; + edition = 201909; + + inputs.utils = { + type = "git"; + uri = "file:///home/zimbatm/go/src/github.com/zimbatm/flake-utils"; + }; + + outputs = { self, nixpkgs, utils }: + utils.lib.eachDefaultSystem (system: + let pkgs = nixpkgs.legacyPackages.${system}; in + rec { + packages.hello = pkgs.hello; + defaultPackage = packages.hello; + apps.hello = utils.lib.mkApp packages.hello; + defaultApp = apps.hello; + } + ); +}