Skip to content

Commit

Permalink
feat: refine scoping rules & names
Browse files Browse the repository at this point in the history
resolves #1

* `self` -> `mod`
* `super` -> `pre`
* external dependencies are merged with `atom` in scope.
* on collision, local references take precedent.
* externals are always available via `atom.extern`.
  • Loading branch information
nrdxp committed Aug 2, 2024
1 parent 6a7ccee commit 1aefe95
Show file tree
Hide file tree
Showing 25 changed files with 55 additions and 55 deletions.
11 changes: 5 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ code without having to perform a full evaluation. This could be used, e.g. to sh
- **Isolation**: Modules are imported into the Nix store, enforcing boundaries and preventing relative path access.
- **Introspection**: Unlike legacy modules, code is specified in its final form instead of as prototypes (functions), leading to much better and simpler introspective analysis.
- **Simplicity**: The system is kept purposefully simple and flexible in order to remain performant and flexible.
- **Scoping**: Each module and member has access to `self`, `super`, `atom`, `pub` and `std`.
- **Scoping**: Each module and member has access to `mod`, `pre`, `atom`, and `std`.
- **Standard Library**: Includes a standard library (`std`) augmented with `builtins`.

## How It Works
Expand All @@ -27,11 +27,10 @@ code without having to perform a full evaluation. This could be used, e.g. to sh
- Subdirectories with `mod.nix`: Treated as nested modules.

2. **Scoping**:
- `self`: Current module, includes `outPath` for accessing non-Nix files.
- `super`: Parent module (if applicable).
- `atom`: Top-level module.
- `mod`: Current module, includes `outPath` for accessing non-Nix files.
- `pre`: Parent module (if applicable).
- `atom`: Top-level module and external dependencies.
- `std`: Standard library and `builtins`.
- `pub`: External resources, such as other atoms, free-form nix expressions, remote sources, etc.

3. **Composition**: Modules are composed recursively, with `mod.nix` contents taking precedence.

Expand All @@ -53,7 +52,7 @@ in
* Break out large functions or code blocks into their own files
* Organize related functionality into subdirectories with their own "mod.nix" files.
* Leverage provided scopes for clean, modular code.
* Use `"${self}"` when needing to access non-Nix files within a module.
* Use `"${mod}/foo.nix"` when needing to access non-Nix files within a module.

## Future Work

Expand Down
25 changes: 13 additions & 12 deletions default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,17 @@ let

in
{
pub ? { },
extern ? { },
}:
dir:
let
std = compose { } ./std // builtins;
atom' = builtins.removeAttrs (extern // atom // { inherit extern; }) [
"atom"
(baseNameOf dir)
];
atom = fix (
f: super: dir:
f: pre: dir:
let
contents = builtins.readDir dir;

Expand All @@ -31,18 +35,15 @@ let

scope = scopedImport (
{
inherit atom std;
self = self // {
inherit std;
atom = atom';
mod = builtins.removeAttrs self [ "mod" ] // {
outPath = filterMod dir;
};
}
// cond {
_if = super != { };
inherit super;
}
// cond {
_if = pub != { };
inherit pub;
_if = pre != { };
inherit pre;
}
);

Expand All @@ -57,8 +58,8 @@ let
${name} = f (
self
// cond {
_if = super != { };
inherit super;
_if = pre != { };
inherit pre;
}
) path;
}
Expand Down
2 changes: 1 addition & 1 deletion dev/pkgs.nix
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
let
inherit (pub.pins) nixpkgs;
inherit (atom.pins) nixpkgs;
in
import nixpkgs { }
2 changes: 1 addition & 1 deletion dev/shell.nix
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
pkgs ? self.pkgs,
pkgs ? mod.pkgs,
}:
pkgs.mkShell {
packages = with pkgs; [
Expand Down
2 changes: 1 addition & 1 deletion shell.nix
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
let
dev = import ./. {
pub = {
extern = {
pins = import ./npins;
};
} ./dev;
Expand Down
2 changes: 1 addition & 1 deletion test/bld/bar/mod.nix
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
foo = 1;
bar = atom.foo + 2;
baz = self.bar + 4;
baz = mod.bar + 4;
}
4 changes: 2 additions & 2 deletions test/bld/bar/next/mod.nix
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{

g = super.foo + 4;
h = self.g + 3;
g = pre.foo + 4;
h = mod.g + 3;
}
2 changes: 1 addition & 1 deletion test/bld/buzz/bar/mod.nix
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
foo = 1;
bar = atom.foo + 2;
baz = self.bar + 4;
baz = mod.bar + 4;
}
4 changes: 2 additions & 2 deletions test/bld/buzz/bar/next/mod.nix
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{

g = super.foo + 4;
h = self.g + 3;
g = pre.foo + 4;
h = mod.g + 3;
}
2 changes: 1 addition & 1 deletion test/bld/buzz/fuzz/bar/mod.nix
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
foo = 1;
bar = atom.foo + 2;
baz = self.bar + 4;
baz = mod.bar + 4;
}
4 changes: 2 additions & 2 deletions test/bld/buzz/fuzz/bar/next/mod.nix
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{

g = super.foo + 4;
h = self.g + 3;
g = pre.foo + 4;
h = mod.g + 3;
}
4 changes: 2 additions & 2 deletions test/bld/buzz/fuzz/mod.nix
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
foo = 1;
bar = super.bar + 2;
baz = self.bar + 4;
bar = pre.bar + 2;
baz = mod.bar + 4;
}
4 changes: 2 additions & 2 deletions test/bld/buzz/fuzz/next/mod.nix
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{

g = super.foo + 4;
h = self.g + 3;
g = pre.foo + 4;
h = mod.g + 3;
}
2 changes: 1 addition & 1 deletion test/bld/buzz/fuzz/wuzz/bar/mod.nix
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
foo = 1;
bar = atom.foo + 2;
baz = self.bar + 4;
baz = mod.bar + 4;
}
4 changes: 2 additions & 2 deletions test/bld/buzz/fuzz/wuzz/bar/next/mod.nix
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{

g = super.foo + 4;
h = self.g + 3;
g = pre.foo + 4;
h = mod.g + 3;
}
2 changes: 1 addition & 1 deletion test/bld/buzz/fuzz/wuzz/cuzz/bar/mod.nix
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
foo = 1;
bar = atom.foo + 2;
baz = self.bar + 4;
baz = mod.bar + 4;
}
4 changes: 2 additions & 2 deletions test/bld/buzz/fuzz/wuzz/cuzz/bar/next/mod.nix
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{

g = super.foo + 4;
h = self.g + 3;
g = pre.foo + 4;
h = mod.g + 3;
}
2 changes: 1 addition & 1 deletion test/bld/buzz/fuzz/wuzz/cuzz/mod.nix
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
foo = 1;
bar = atom.foo + 2;
baz = self.bar + 4;
baz = mod.bar + 4;
}
4 changes: 2 additions & 2 deletions test/bld/buzz/fuzz/wuzz/cuzz/next/mod.nix
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{

g = super.foo + 4;
h = self.g + 3;
g = pre.foo + 4;
h = mod.g + 3;
}
4 changes: 2 additions & 2 deletions test/bld/buzz/fuzz/wuzz/mod.nix
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
foo = 1;
bar = super.bar + 2;
baz = self.bar + 4;
bar = pre.bar + 2;
baz = mod.bar + 4;
}
4 changes: 2 additions & 2 deletions test/bld/buzz/fuzz/wuzz/next/mod.nix
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{

g = super.foo + 4;
h = self.g + 3;
g = pre.foo + 4;
h = mod.g + 3;
}
4 changes: 2 additions & 2 deletions test/bld/buzz/mod.nix
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
foo = 1;
bar = super.foo + 2;
baz = self.bar + 4;
bar = pre.foo + 2;
baz = mod.bar + 4;
}
4 changes: 2 additions & 2 deletions test/bld/buzz/next/mod.nix
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{

g = super.foo + 4;
h = self.g + 3;
g = pre.foo + 4;
h = mod.g + 3;
}
4 changes: 2 additions & 2 deletions test/bld/mod.nix
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
foo = 1;
bar = atom.foo + 2;
baz = self.bar + 4;
baz = mod.bar + 4;
test = std.set.filterMap;
x = builtins.readFile "${self}/bum";
x = builtins.readFile "${mod}/bum";
}
4 changes: 2 additions & 2 deletions test/bld/next/mod.nix
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{

g = super.foo + 4;
h = self.g + 3;
g = pre.foo + 4;
h = mod.g + 3;
}

0 comments on commit 1aefe95

Please sign in to comment.