diff --git a/flake.nix b/flake.nix index d5a01f9f..8236fe0b 100644 --- a/flake.nix +++ b/flake.nix @@ -204,6 +204,7 @@ (id: _: self.packages.x86_64-linux."phony-oci-bundles.${id}") self.modules; lib.mkBundle = import ./pkgs/bundle/api.nix { inherit pkgs self; }; + lib.mkReplitShell = import ./pkgs/replit-shell { 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; diff --git a/pkgs/moduleit/shell-package.nix b/pkgs/moduleit/shell-package.nix index bfa8f442..b3f6fd00 100644 --- a/pkgs/moduleit/shell-package.nix +++ b/pkgs/moduleit/shell-package.nix @@ -34,7 +34,10 @@ let + lib.optionalString (key == "PATH") "\${PATH:+:}\"$PATH\"") ordered.result + "\n"; in -runCommand name { } '' +runCommand name +{ + passthru.replitModuleId = moduleId; +} '' 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" diff --git a/pkgs/replit-shell/default.nix b/pkgs/replit-shell/default.nix new file mode 100644 index 00000000..248f8c6c --- /dev/null +++ b/pkgs/replit-shell/default.nix @@ -0,0 +1,59 @@ +{ pkgs }: +{ packages ? [ ], shellHook ? "", name ? "replit-devshell" }: +let + inherit (pkgs) lib; + # Explicit module metadata separates module environments from ordinary packages. + inputs = lib.partition (pkg: pkg ? replitModuleId) (map + (pkg: + assert lib.assertMsg (lib.isDerivation pkg) + "mkReplitShell: each packages element must be a derivation"; + pkg) + packages); + moduleHook = module: '' + source ${lib.escapeShellArg "${module}/nix-support/replit-env"} + ''; + closure = map (entry: entry.pkg) (builtins.genericClosure { + startSet = map (pkg: { key = toString pkg; inherit pkg; }) inputs.wrong; + operator = entry: map + (pkg: { key = toString pkg; inherit pkg; }) + (entry.pkg.propagatedBuildInputs or [ ]); + }); + dirs = output: suffix: lib.unique (map (pkg: "${output pkg}/${suffix}") closure); + libDirs = dirs lib.getLib "lib"; + devDirs = dirs lib.getDev "include"; + pkgConfigDirs = lib.unique (dirs lib.getDev "lib/pkgconfig" ++ dirs lib.getLib "lib/pkgconfig"); + # Ordinary packages contribute directories only; their stdenv setup hooks never run. + exportDirs = var: separator: prefix: paths: lib.optionalString (paths != [ ]) '' + replit_shell_value= + for replit_shell_dir in ${lib.escapeShellArgs paths}; do + if [[ -d "$replit_shell_dir" ]]; then + replit_shell_value+="''${replit_shell_value:+${separator}}${prefix}$replit_shell_dir" + fi + done + if [[ -n "$replit_shell_value" ]]; then + export ${var}="$replit_shell_value''${${var}:+${separator}}''${${var}-}" + fi + unset replit_shell_value replit_shell_dir + ''; +in +builtins.derivation { + inherit name; + outputs = [ "out" ]; + system = pkgs.stdenv.hostPlatform.system; + builder = "${pkgs.bash}/bin/bash"; + args = [ "-c" ": > $out" ]; + SHELL = "${pkgs.bash}/bin/bash"; + # No PATH: the workspace runtime accepts a shell without one and ignores Nix's /path-not-set. + shellHook = lib.concatMapStrings moduleHook (lib.reverseList inputs.right) + + exportDirs "PATH" ":" "" (dirs lib.getBin "bin") + + exportDirs "REPLIT_LD_LIBRARY_PATH" ":" "" libDirs + + exportDirs "LDFLAGS" " " "-L" libDirs + + exportDirs "NIX_LDFLAGS" " " "-L" libDirs + + exportDirs "PKG_CONFIG_PATH" ":" "" pkgConfigDirs + + exportDirs "PKG_CONFIG_PATH_FOR_TARGET" ":" "" pkgConfigDirs + + exportDirs "XDG_DATA_DIRS" ":" "" (dirs (lib.getOutput "out") "share") + + exportDirs "CFLAGS" " " "-isystem " devDirs + + exportDirs "NIX_CFLAGS_COMPILE" " " "-isystem " devDirs + + exportDirs "GI_TYPELIB_PATH" ":" "" (dirs lib.getLib "lib/girepository-1.0") + + shellHook; +}