Skip to content
This repository has been archived by the owner on Jan 26, 2025. It is now read-only.

Commit

Permalink
More functions; docs; and fixes (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
PedroHLC authored Sep 28, 2023
1 parent 7d2383c commit 22095c8
Show file tree
Hide file tree
Showing 2 changed files with 184 additions and 32 deletions.
104 changes: 102 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ E.g.:
outputs = { nixpkgs, yafas, ... }@inputs:
yafas.allLinux nixpkgs
({ pkgs, system }: { package.default = whatever1; })
({ pkgs, system }: { packages.default = whatever1; })
|> yafas.withAarch64Darwin nixpkgs
(_prev: { pkgs, ... }: { package.default = whatever2; })
(_prev: { pkgs, ... }: { packages.default = whatever2; })
|> yafas.withOverlays
(_prev: { default = whatever3; })
|> yafas.withOverlay "cool"
Expand All @@ -23,3 +23,103 @@ E.g.:
(prev: prev // { myLibs = prev.myLibs // { }; });
}
```

A minimalist example:

```nix
{
inputs = whatever0;
outputs = { nixpkgs, yafas, ... }@inputs:
yafas.withSystem "riscv64-linux" nixpkgs
(_prev: { pkgs, ... }: { packages.default = whatever2; })
{
myLibs = whatever1;
}
}
```

## Documentation

### Supported systems

We use [github.com:nix-systems/default](https://github.com/nix-systems/default) as `inputs.systems`, feel free to override it.

### Constructors

- `allDarwin: nixpkgs -> applier -> ouputs`
- `allLinux: nixpkgs -> applier -> ouputs`
- `allSystems: system[] -> nixpkgs -> applier -> ouputs`

Where `applier` is the lambda `{ pkgs, system }: { package.default = pkgs.callPackage ... { }; }`.

NOTE: These function will do a system-name injection, so all the outputs here have to be system-specific:
```
packages.default = x; -> packages.${system}.default = x;
formatter = y; -> formatter.${system} = y;
```

### Helpers

- `map: applier -> outputs -> x`

Where `applier` is a map applier.

### With per-system

- Multiple:
- `withSystems: system[] -> nixpkgs -> applier -> outputs -> outputs`
- `withDarwin: nixpkgs -> applier -> outputs -> outputs`
- `withLinux: nixpkgs -> applier -> outputs -> outputs`

- Single:
- `withSystem: system -> nixpkgs -> applier -> outputs -> outputs`
- `withAarch64Linux: nixpkgs -> applier -> outputs -> outputs`
- `withAMD64Linux: nixpkgs -> applier -> outputs -> outputs`
- `withAarch64Darwin: nixpkgs -> applier -> outputs -> outputs`
- `withAMD64Darwin: nixpkgs -> applier -> outputs -> outputs`

Where `applier` is the lambda `_prevOutputs: { pkgs, system }: { package.default = pkgs.callPackage ... { }; }`.

NOTE: Read [Constructors](#Constructors)' notes.

### With per-system products

- Generic:
- `withNestedSystem: outputName -> applier -> outputs -> outputs`

- Specific:
- `withApps: applier -> outputs -> outputs`
- `withDevShells: applier -> outputs -> outputs`
- `withLegacyPackages: applier -> outputs -> outputs`
- `withPackages: applier -> outputs -> outputs`
- `withFormatter: applier -> outputs -> outputs`

Where `applier` is the lambda `_prevOutputs: { pkgs, system }: { default = pkgs.callPackage ... { }; }`.

### With universals

- Generic:
- `withUniversals: applier -> outputs -> outputs`
- `withUniversal: outputName -> applier -> ouputs -> ouputs`

- Specific:
- `withHomeManagerModules: applier -> ouputs -> ouputs`
- `withNixOSModules: applier -> ouputs -> ouputs`
- `withOverlays: applier -> ouputs -> ouputs`
- `withSchemas: applier -> ouputs -> ouputs`

Where `applier` is the lambda `_prevOutputs: { <name> = <value>; }`.

### With universals products

- Generic:
- `withNestedUniversal: outputName -> name -> outputs -> outputs`

- Specific:
- `withHomeManagerModule: name -> outputs -> outputs`
- `withNixOSModule: name -> outputs -> outputs`
- `withOverlay: name -> outputs -> outputs`
- `withSchema: name -> outputs -> outputs`

Where `applier` is the lambda `_prevOutputs: { <name> = <value>; }`.
112 changes: 82 additions & 30 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,13 @@
outputs = { self, systems }:
let
# lib
inherit (builtins) filter foldl' mapAttrs stringLength substring zipAttrsWith;
inherit (builtins) attrNames elemAt filter foldl' head isAttrs
length mapAttrs stringLength substring zipAttrsWith;

update =
a: b: if isAttrs b then recursiveUpdate a b else b;

# nixpkgs clones
hasSuffix = suffix: content:
let
lenContent = stringLength content;
Expand All @@ -13,68 +18,115 @@
lenContent >= lenSuffix
&& substring (lenContent - lenSuffix) lenContent content == suffix;

recursiveUpdateUntil = pred: lhs: rhs:
let
f = attrPath:
zipAttrsWith (n: values:
let here = attrPath ++ [ n ]; in
if length values == 1
|| pred here (elemAt values 1) (head values) then
head values
else
f here values
);
in
f [ ] [ rhs lhs ];

recursiveUpdate = lhs: rhs:
recursiveUpdateUntil (path: lhs: rhs: !(isAttrs lhs && isAttrs rhs)) lhs rhs;

# main functions
support = targets: accu: nixpkgs: applier:
support = targets: nixpkgs: applier:
let
eachSystem = system:
let
nestSystem = _: value: { ${system} = value; };

systemOutputs = applier {
inherit system;
pkgs = nixpkgs.legacyPackages.${system};
};
in
mapAttrs nestSystem systemOutputs;

in
zipAttrsWith
(_: values: foldl' (a: b: a // b) { } values)
(_: values: foldl' update { } values)
(map eachSystem targets);

support' = target: nixpkgs: applier: accu:
support [ target ] accu nixpkgs (applier accu);
support' = targets: nixpkgs: applier: accu:
update accu (support targets nixpkgs (applier accu));

withUniversal = output:
applier: accu: accu // ({ "${output}" = (accu.${output} or { }) // applier accu; });
applier: accu: accu // ({
"${output}" =
let
new = applier accu;
in
if isAttrs new then
update (accu.${output} or { }) new
else new;
});

withNestedUniversal = output: name:
applier: accu: accu // ({ "${output}" = (accu.${output} or { }) // { "${name}" = applier accu; }; });

findSystems = accu:
attrNames (accu.legacyPackages or accu.packages or accu.apps or accu.formatter or accu.devShells);

withNestedSystem = name: nixpkgs: applier: accu:
support (findSystems accu) accu nixpkgs
({ pkgs, system }@combo: {
"${name}" = update accu.${name}.${system} (applier accu combo);
});

# system list
importedSystems = import systems;
linuxes = filter (hasSuffix "-linux") importedSystems;
darwins = filter (hasSuffix "-darwin") importedSystems;
in
{
# constructors
allLinux = support linuxes { };
allDarwin = support darwins { };
allSystems = support (linuxes ++ darwins) { };

# pkgs-dependent
withDarwin = support darwins;
withLinux = support linuxes;

withSystem = support';
withAMD64Linux = support' "x86_64-linux";
withAarch64Linux = support' "aarch64-linux";
withAMD64Darwin = support' "x86_64-darwin";
withAarch64Darwin = support' "aarch64-darwin";

# universal attrset
# Constructors
allLinux = support linuxes;
allDarwin = support darwins;
allSystems = support (linuxes ++ darwins);

# With per-system
withSystems = support';
withDarwin = support' darwins;
withLinux = support' linuxes;

withSystem = target: support' [ target ];
withAarch64Linux = support' [ "aarch64-linux" ];
withAMD64Linux = support' [ "x86_64-linux" ];
withAarch64Darwin = support' [ "aarch64-darwin" ];
withAMD64Darwin = support' [ "x86_64-darwin" ];

# With per-system products
inherit withNestedSystem;
withApps = withNestedSystem "apps";
withDevShells = withNestedSystem "devShells";
withLegacyPackages = withNestedSystem "legacyPackages";
withPackages = withNestedSystem "packages";
withFormatter = nixpkgs: applier: accu:
support (findSystems accu) accu nixpkgs
(combo: { formatter = applier combo; });

# With universals
withUniversals = applier: accu: accu // (applier accu);
inherit withUniversal;
withHomeManagerModules = withUniversal "homeManagerModules";
withNixOSModules = withUniversal "nixosModules";
withOverlays = withUniversal "overlays";
withSchemas = withUniversal "schemas";
withNixOSModules = withUniversal "nixosModules";
withHomeManagerModules = withUniversal "homeManagerModules";
withUniversals = applier: accu: accu // (applier accu);

# universal products
# With universals products
inherit withNestedUniversal;
withHomeManagerModule = withNestedUniversal "homeManagerModules";
withNixOSModule = withNestedUniversal "nixosModules";
withOverlay = withNestedUniversal "overlays";
withSchema = withNestedUniversal "schemas";
withNixOSModule = withNestedUniversal "nixosModules";
withHomeManager = withNestedUniversal "homeManagerModules";

# helper
# Helpers
map = applier: accu: applier accu;
};
}

0 comments on commit 22095c8

Please sign in to comment.