---
title: "Dynamic version for Nix derivations"
description: "Replit derives a Nix derivation's version automatically from the git short SHA instead of editing default.nix by hand for every release."
url: "https://swe.quest/articles/dynamic-version-for-nix-derivations-ecd33aca5a26fee8"
canonical: "https://replit.com/blog/nix_dynamic_version"
source: "Replit"
published: "2021-06-25"
difficulty: "medium"
kind: "How-to"
reading_minutes: 8
tags: ["build-systems","ci-cd","functional-programming"]
---

# Dynamic version for Nix derivations

A post on [Replit](https://swe.quest/sources/replit.md), published 2021-06-25, 8 min read, How-to. Original: [https://replit.com/blog/nix\_dynamic\_version](https://replit.com/blog/nix_dynamic_version)

## Summary

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.

Written by swe.quest, not by the author.

**Difficulty:** Medium: assumes a working developer; shows how it is built or done

## Topics

- [Build systems](https://swe.quest/tags/build-systems.md) (Engineering practices)
- [CI/CD](https://swe.quest/tags/ci-cd.md) (Reliability & operations)
- [Functional programming](https://swe.quest/tags/functional-programming.md) (Languages)

## Prerequisites

- Nix derivations basics
- git commit SHAs

## Keywords

Nix, nixpkgs, runCommand, copyPathToStore, fetchGit, Replit

## Related posts

- [How we went from supporting 50 languages to all of them](https://swe.quest/articles/how-we-went-from-supporting-50-languages-to-all-of-them-937edb0cd8b8b0b8.md): Replit · 2021-05-24 · Easy · 3 min read · Build systems, Containers, Developer tools
  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.
- [Building a web app with Nix (Because why not?)](https://swe.quest/articles/building-a-web-app-with-nix-because-why-not-53b9f7ab22ec7d0e.md): Replit · 2021-07-16 · Medium · 16 min read · Functional programming, Build systems, Python
  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.
- [Faster Nix Repl Startup](https://swe.quest/articles/faster-nix-repl-startup-b982aebd762e09bf.md): Replit · 2021-10-13 · Medium · 3 min read · Caching, Performance engineering, Developer tools
  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.
- [Why We’re Moving on From Nix](https://swe.quest/articles/why-we-re-moving-on-from-nix-3cb8ef181b1a528f.md): Railway · 2025-03-04 · Medium · 5 min read · Build systems, Containers, Go, Deployment, Caching
  Railway replaced its Nix-based builder Nixpacks with Railpack, a Go rewrite that generates BuildKit LLB graphs directly instead of linear Dockerfiles.
- [DotSlash: Simplified executable deployment](https://swe.quest/articles/dotslash-simplified-executable-deployment-e29283bb97f83f66.md): Meta Engineering · 2024-02-06 · Medium · 13 min read · Developer tools, Build systems, Rust, CI/CD, Version control
  DotSlash, now open source and written in Rust, replaces platform-specific executables in source control with a small text file (shebang \`#!/usr/bin/env dotslash\` plus a lenient-JSON manifest) listing per-platform size, hash, format and providers.

Browse: [Latest posts](https://swe.quest/index.md) · [Topics](https://swe.quest/tags.md) · [Sources](https://swe.quest/sources.md) · [Topic map](https://swe.quest/map.md) · [Site guide (llms.txt)](https://swe.quest/llms.txt)
