Skip to content

In development. Expect rough edges and changes. The source code is not public yet.

Replit 3 min read Case study mediumAdept

Faster Nix Repl Startup

Replit removed the multi-second (sometimes tens of seconds) "Loading Nix environment" delay on repl startup by caching the environment variables exported by nix-shell after evaluating replit.nix. Because each repl gets a seeded Nix store containing most of nixpkgs, evaluation rarely adds new store paths, so the cached PATH and other variables can be persisted to the repl's filesystem and reused across restarts. The cache is invalidated whenever a cached variable references a Nix store path that no longer exists, falling back to full evaluation so correctness is preserved.

Assumes: Nix basicsenvironment variables and PATH

Related posts

  • How we went from supporting 50 languages to all of them

    Replit easyNovice

    Replit replaced its ever-growing monolithic Docker OS image with Nix, giving every repl access to over 30,000 OS packages through a declarative `replit.nix` file. The packages are prebuilt into a roughly 1 TB shared Nix store disk image that is mounted at `/nix` in every repl, so nothing needs to be downloaded. Nix's content-addressed store lets many versions coexist without conflicts, and each environment exposes only the packages it declares. Planned work includes `.replit` configuration for things like LSP and caching nix-shell environments to remove rebuild delay on run, which an edit notes has since shipped.

  • Building a web app with Nix (Because why not?)

    Replit mediumAdept

    A toy web app is built in the Nix language by having a Python HTTP server shell out to `nix eval`, passing the request path and body as `--arg` values. Routing is done with attribute sets and `builtins.hasAttr`. A TODO API is backed by the Replit database, using `pkgs.runCommand` with curl to get around Nix's lack of side effects. Along the way it explains Nix language basics, including `let..in`, laziness, default arguments and `import`, plus why a dummy `builtins.currentTime` attribute is needed to defeat derivation caching.

  • Replit²

    Replit mediumAdept

    A Replit Nix repl can act as a code-execution backend: a Koa.js server spawns a Python child process and feeds user code through stdin, using code.InteractiveInterpreter, which avoids shell injection and keeps crashes from taking down the server. A second repl works as a proxy that load-balances across forked compute nodes, tracking per-node concurrent calls in Replit Database with a usage threshold and HTML-escaping input. A simple web component front end calls the cluster's API. A security section notes the limits of this sandboxing approach.

  • Dynamic version for Nix derivations

    Replit mediumAdept

    Replit derives a Nix derivation's version automatically from the git short SHA instead of editing default.nix by hand for every release. The final approach copies the whole source tree, including .git, into the store with copyPathToStore and runs `git rev-parse --short HEAD` inside a runCommand. It then reads the output with builtins.readFile, strips the trailing newline and prefixes a digit to satisfy Nix's version-format rule. The write-up walks through the failures along the way (sandboxed builds can't see the host repo, fetchGit drops .git, runCommand outputs a file) and closes with lessons on Nix laziness, purity and attribute sets.

  • Introducing the Python package cache

    Replit mediumAdept

    Replit pre-populates pip's cache with the most popular Python packages and pre-built wheels so UPM can skip most downloads and compilation when a repl starts. A read-only snapshot of the cache is bind-mounted into each repl and overlay-mounted onto ~/.cache/pip with a per-repl writable upper directory, giving copy-on-write isolation that prevents cross-repl cache poisoning while still letting users add packages. Because pip's cache is content-addressable, shared files can be found by hash and integrity-checked. The cache is rebuilt weekly from PyPI download stats and cut average install time by about 40% on Always On repls, with Maven and npm support planned next.

The quest for software engineering knowledge.

Look:
Layout: