ℹ️ Select 'Choose Exercise', or randomize 'Next Random Exercise' in selected language.

Choose Exercise:
Timer 00:00
WPM --
Score --
Acc --
Correct chars --

Nix Package Version Pinning

Nix

Goal -- WPM

Ready
Exercise Algorithm Area
1{
2pkgs,
3... # other inputs
4}: {
5# This function demonstrates how to pin a specific version of a package
6# to ensure reproducible builds. It takes the Nixpkgs set as input.
7
8# Define the package we want to pin.
9# We'll use 'hello' as a simple example.
10pinnedHello = pkgs.hello.overrideAttrs (oldAttrs: {
11# Override the version attribute to a specific, known-good version.
12# This is crucial for reproducibility.
13version = "2.12";
14
15# You might also need to override the src if the version change
16# affects the source URL or hash.
17# src = fetchurl {
18# url = "https://ftp.gnu.org/gnu/hello/hello-2.12.tar.gz";
19# sha256 = "0000000000000000000000000000000000000000000000000000";
20# };
21
22# Ensure that any patches or build inputs are compatible with the pinned version.
23# In this simple case, no further overrides are strictly necessary for 'hello'.
24});
25
26# Return the pinned package.
27# This can then be used in your Nix configurations or derivations.
28# For example: "${pinnedHello}/bin/hello"
29# Or: buildInputs = [ pinnedHello ];
30return {
31inherit pinnedHello;
32};
33}
Algorithm description viewbox

Nix Package Version Pinning

Algorithm description:

This Nix expression demonstrates how to pin a specific version of a package, like 'hello', to guarantee reproducible builds. By overriding the package's attributes, particularly the `version` and potentially `src`, we ensure that the exact same code is built every time, regardless of upstream changes in the default Nixpkgs channel. This is fundamental for maintaining stable development and deployment environments.

Algorithm explanation:

The provided Nix code utilizes the `overrideAttrs` function to create a new derivation for the 'hello' package, but with a explicitly specified `version`. This ensures that even if the default Nixpkgs channel is updated to a newer version of 'hello', this specific derivation will always build version 2.12. The `version` attribute is the primary mechanism for pinning. If the source code location or its hash changes with a new version, the `src` attribute would also need to be overridden with a `fetchurl` or `fetchgit` pointing to the exact source code and its corresponding hash. This approach guarantees that the build is deterministic and reproducible, a core tenet of Nix's philosophy. The time and space complexity are effectively constant for this pinning operation itself, as it primarily involves referencing existing package definitions and metadata. The correctness relies on Nix's content-addressable store and its ability to build from exact source and configuration.

Pseudocode:

FUNCTION pin_package(package_set):
  DEFINE pinned_package = package_set.package.overrideAttrs(
    version = "specific_version"
    # Optionally override src if needed
  )
  RETURN { pinned_package = pinned_package }
END FUNCTION