Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,29 @@ To build only a few modules which you can control by editing the `dev-module-ids
nix build .#custom-bundle
```

To build a specific module, for example `python-3.10`, you can do:
To build a specific module's package (a directory with its setup hook and module JSON), for example `python-3.10`, you can do:

```
nix build .#'"python-3.10"'
```

### Using modules in a devShell

Repl flakes list module packages in `mkShell`. When the shell starts, each one
applies the module's environment and PATH (the same one a Repl gets, with
`REPL_HOME` defaulting to the working directory) and adds its metadata directory
to `REPLIT_MODULES`. If two modules set the same variable, the first one listed wins:

```nix
devShells.x86_64-linux.default = pkgs.mkShell {
packages = [
nixmodules.packages.x86_64-linux.nodejs-24
nixmodules.packages.x86_64-linux."python-3.12"
pkgs.ffmpeg
];
};
```

To build a disk with only a few modules for development purposes:

```
Expand Down
5 changes: 1 addition & 4 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -184,11 +184,8 @@
phony-oci-bundles = builtins.mapAttrs
(id: _: self.packages.x86_64-linux."phony-oci-bundles.${id}")
self.modules;
shellPackages.x86_64-linux = pkgs.lib.mapAttrs (_: module: module.shellPackage) self.modules;
deploymentShellPackages.x86_64-linux = pkgs.lib.mapAttrs (_: module: module.shellPackage) self.deploymentModules;
lib.mkBundle = import ./pkgs/bundle/api.nix { inherit pkgs self; };
lib.mkToolchain = import ./pkgs/toolchain { inherit pkgs self; };
checks.x86_64-linux.flake-toolchain = import ./pkgs/toolchain/tests.nix { inherit pkgs; };
checks.x86_64-linux.module-shell = import ./pkgs/moduleit/tests.nix { inherit pkgs; };
checks.x86_64-linux.module-registry =
assert import ./scripts/module-registry-test.nix;
assert builtins.attrNames self.modules == builtins.attrNames self.deploymentModules;
Expand Down
8 changes: 3 additions & 5 deletions pkgs/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ rec {
path = self.checks.x86_64-linux.module-registry;
}
{
name = "flake-toolchain";
path = self.checks.x86_64-linux.flake-toolchain;
name = "module-shell";
path = self.checks.x86_64-linux.module-shell;
}
{
name = "pip-user-site";
Expand Down Expand Up @@ -123,8 +123,6 @@ rec {
};

}
// modules
// mapAttrs' (id: module: nameValuePair "shell.${id}" module.shellPackage) self.modules
// mapAttrs' (id: module: nameValuePair "deployment-shell.${id}" module.shellPackage) self.deploymentModules
// mapAttrs (_: module: module.shellPackage) modules
// prefixedPackages "deploymentModules" self.deploymentModules
// prefixedPackages "phony-oci-bundles" phony-oci-bundles
12 changes: 5 additions & 7 deletions pkgs/moduleit/entrypoint.nix
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,18 @@ let
result =
if deployment then module.config.replit.buildDeploymentModule
else module.config.replit.buildModule;
packages = module.config.replit.packages
++ pkgs.lib.optionals (!deployment) module.config.replit.dev.packages;
in
pkgs.lib.lazyDerivation {
derivation = result;
passthru = {
moduleId = module.config.id;
moduleConfig = result.moduleConfig;
runtimePackages = packages;
} // pkgs.lib.optionalAttrs (!deployment) {
shellPackage = pkgs.callPackage ./shell-package.nix {
name = "replit-${if deployment then "deployment-" else ""}shell-${module.config.id}";
inherit packages;
name = "replit-shell-${module.config.id}";
moduleId = module.config.id;
module = result;
packages = module.config.replit.packages ++ module.config.replit.dev.packages;
env = result.moduleConfig.env;
passthru.module = result;
};
};
}
32 changes: 20 additions & 12 deletions pkgs/moduleit/shell-package.nix
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
, runCommand
, writeText
, name
, moduleId
, module
, packages
, env
, passthru ? { }
}:
let
variablePattern = "[a-zA-Z_][a-zA-Z0-9_]*";
Expand Down Expand Up @@ -33,14 +34,21 @@ let
+ lib.optionalString (key == "PATH") "\${PATH:+:}\"$PATH\"")
ordered.result + "\n";
in
runCommand name
{
inherit passthru;
}
''
mkdir -p "$out/nix-support"
cp ${writeText "${name}-hook" hook} "$out/nix-support/setup-hook"
# Keep runtime references without running their compiler build hooks.
printf '%s\n' ${lib.escapeShellArg (lib.concatMapStringsSep "\n" toString packages)} \
> "$out/nix-support/runtime-dependencies"
''
runCommand name { } ''
mkdir -p "$out/nix-support" "$out/share/replit/modules"
# The IDE finds module metadata through REPLIT_MODULES; its store paths keep LSPs and runners alive.
cp ${module} "$out/share/replit/modules/${moduleId}.json"
{
cat ${writeText "${name}-env" hook}
echo "export REPLIT_MODULES=\"$out/share/replit/modules\''${REPLIT_MODULES:+:\$REPLIT_MODULES}\""
} > "$out/nix-support/replit-env"
# Apply the env when the shell starts, not when nix captures it in the build sandbox.
# Prepending makes the first-listed module win, both for PATH and plain variables.
cat > "$out/nix-support/setup-hook" <<EOF
shellHook="source $out/nix-support/replit-env
\''${shellHook-}"
EOF
# Keep runtime references without running their compiler build hooks.
printf '%s\n' ${lib.escapeShellArg (lib.concatMapStringsSep "\n" toString packages)} \
> "$out/nix-support/runtime-dependencies"
''
152 changes: 152 additions & 0 deletions pkgs/moduleit/tests.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
{ pkgs }:
let
inherit (pkgs) lib;
runtime = pkgs.runCommand "module-shell-test-runtime" { } ''
mkdir -p "$out/bin" "$out/nix-support"
printf '#!/bin/sh\nexit 0\n' > "$out/bin/runtime"
chmod +x "$out/bin/runtime"
cat > "$out/nix-support/setup-hook" <<'HOOK'
: "$NIX_CC"
echo "A runtime-only build hook was sourced" >&2
return 1
HOOK
'';
ide = pkgs.writeText "module-shell-test-ide" "IDE-only dependency";
definition = {
id = "fixture";
name = "Fixture";
replit = {
packages = [ runtime ];
env = {
A_DEPENDENT = "$Z_BASE/child";
Z_BASE = "$REPL_HOME/base";
BRACED = "\${Z_BASE}/braced";
LITERAL = "$(touch /tmp/nixmodules-shell-injection) `false` \" ' \\";
PYTHONPATH = "$PYTHONPATH/suffix";
MISSING = "$NOT_PRESENT";
EMPTY = "";
MULTILINE = "first=line\nsecond=line";
MODE = "deployment";
PATH = "$Z_BASE/bin";
};
runners.fixture = {
name = "Fixture";
language = "fixture";
start = "${runtime}/bin/runtime $file";
};
dev = {
env.MODE = "development";
languageServers.fixture = {
name = "Fixture server";
language = "fixture";
start = "${ide} $file";
};
};
};
};
mkModule = deployment: configPath: pkgs.callPackage ./entrypoint.nix {
inherit deployment configPath;
pkgs-23_05 = pkgs;
pkgs-24_11 = pkgs;
};
development = mkModule false definition;
deployment = mkModule true definition;
applyAliases = import ../upgrade-map/apply.nix { alias = "fixture"; };
self = {
modules = applyAliases {
fixture = development;
override = mkModule false {
id = "override";
name = "Override";
replit.env.MODE = "last";
};
};
deploymentModules = applyAliases { fixture = deployment; };
};
legacyBundle = pkgs.callPackage ../bundle { inherit self; };
mkBundle = import ../bundle/api.nix { inherit pkgs self; };
deploymentBundle = mkBundle { moduleIds = [ "alias" ]; deployment = true; };
closure = pkgs.closureInfo { rootPaths = [ development.shellPackage ]; };
fails = value: !(builtins.tryEval (builtins.deepSeq value true)).success;
lazyModule = mkModule false {
id = "lazy";
name = "Lazy";
replit.packages = throw "moduleId must not evaluate packages or output paths";
};
cycle = pkgs.callPackage ./shell-package.nix {
name = "cycle";
moduleId = "cycle";
module = pkgs.writeText "cycle" "{}";
packages = [ ];
env = { A = "$B"; B = "$A"; };
};
decoy = pkgs.writeShellApplication { name = "runtime"; text = "exit 1"; };
# What nix develop does: capture the env in a build, then run shellHook in the user's shell.
shellSmoke = pkgs.runCommand "module-shell-smoke"
{
nativeBuildInputs = [ decoy self.modules.alias.shellPackage self.modules.override.shellPackage ];
shellHook = "export USER_HOOK_MODE=$MODE";
} ''
test -z "''${MODE-}"
export REPL_HOME="/tmp/project with spaces"
eval "$shellHook"
test "$MODE" = development
test "$USER_HOOK_MODE" = development
test "$A_DEPENDENT" = "$REPL_HOME/base/child"
test "$(command -v runtime)" = "${runtime}/bin/runtime"
test "$REPLIT_MODULES" = "${development.shellPackage}/share/replit/modules:${self.modules.override.shellPackage}/share/replit/modules"
touch "$out"
'';
in
assert lazyModule.moduleId == "lazy";
assert fails (mkBundle { moduleIds = [ "missing" ]; }).outPath;
assert fails (mkBundle { moduleIds = [ "fixture" "missing" ]; }).outPath;
assert fails (mkBundle { moduleIds = [ "missing" ]; deployment = true; }).outPath;
assert (legacyBundle { moduleIds = [ "fixture" "missing" ]; }).outPath
== (legacyBundle { moduleIds = [ "fixture" ]; }).outPath;
assert (legacyBundle { moduleIds = [ "missing" ]; }).outPath
== (legacyBundle { moduleIds = [ ]; }).outPath;
assert (legacyBundle { moduleIds = [ "alias" "missing" ]; deployment = true; }).outPath
== deploymentBundle.outPath;
assert fails cycle.outPath;
pkgs.runCommand "module-shell-check"
{
nativeBuildInputs = [ pkgs.jq ];
} ''
test -f ${shellSmoke}
test "$(readlink ${deploymentBundle}/alias)" = "${deployment}"
jq -e --arg path "${deployment}" '.alias.path == $path' ${deploymentBundle}/etc/nixmodules/modules.json
jq -e '.env.MODE == "development" and .languageServers != {}' ${development}
jq -e '.env.MODE == "deployment" and .languageServers == {}' ${deployment}
cmp ${development} ${development.shellPackage}/share/replit/modules/fixture.json
grep -Fx ${runtime} ${closure}/store-paths
grep -Fx ${ide} ${closure}/store-paths
if grep -Fx ${pkgs.jq} ${closure}/store-paths; then
echo "Module package includes a JSON encoder dependency" >&2
exit 1
fi
(
export REPL_HOME="/tmp/project with spaces"
export PATH="/original/path"
export PYTHONPATH="/inherited"
unset REPLIT_MODULES
source ${development.shellPackage}/nix-support/replit-env
test "$A_DEPENDENT" = "$REPL_HOME/base/child"
test "$BRACED" = "$REPL_HOME/base/braced"
test "$LITERAL" = ${lib.escapeShellArg definition.replit.env.LITERAL}
test "$PYTHONPATH" = "/inherited/suffix"
test "$MISSING" = ""
test "$EMPTY" = ""
test "$MULTILINE" = ${lib.escapeShellArg definition.replit.env.MULTILINE}
test "$MODE" = development
test "$PATH" = "$REPL_HOME/base/bin:${runtime}/bin:/original/path"
test "$REPLIT_MODULES" = "${development.shellPackage}/share/replit/modules"
)
(
unset REPL_HOME PYTHONPATH
source ${development.shellPackage}/nix-support/replit-env
test "$REPL_HOME" = "$PWD"
test "$PYTHONPATH" = "/suffix"
)
touch "$out"
''
64 changes: 0 additions & 64 deletions pkgs/toolchain/default.nix

This file was deleted.

Loading