Skip to content

Instantly share code, notes, and snippets.

@Malix-Labs
Last active July 11, 2026 12:29
Show Gist options
  • Select an option

  • Save Malix-Labs/d4157e47f0f7175242cf717b936af5e3 to your computer and use it in GitHub Desktop.

Select an option

Save Malix-Labs/d4157e47f0f7175242cf717b936af5e3 to your computer and use it in GitHub Desktop.
Nix Replace Coreutils with Uutils

Nix Lib Coreutils

Nix Library for Coreutils

Setup

Flakes

  1. inputs.lib-coreutils = {
      url = "https://gist.github.com/Malix-Labs/eec30a668f03ca4ad1060c68c3f5c442";
      type = "git";
      inputs.nixpkgs-lib.follows = "nixpkgs";
    };
  2. nix flake lock
  1. tack add lib-coreutils https://gist.github.com/Malix-Labs/eec30a668f03ca4ad1060c68c3f5c442
  2. lib-coreutils = import (import ./.tack).lib-coreutils { inherit (pkgs) lib; };
  1. npins add git https://gist.github.com/Malix-Labs/eec30a668f03ca4ad1060c68c3f5c442 -n lib-coreutils
  2. lib-coreutils = import (import ./npins).lib-coreutils { inherit (pkgs) lib; };
  1. niv add gist -g eec30a668f03ca4ad1060c68c3f5c442 -n lib-coreutils
  2. lib-coreutils = import (import ./nix/sources.nix).lib-coreutils { inherit (pkgs) lib; };

builtins.fetchGit

lib-coreutils = import (builtins.fetchGit {
  url = "https://gist.github.com/Malix-Labs/eec30a668f03ca4ad1060c68c3f5c442.git";
  rev = "<COMMIT_HASH>";
}) { inherit (pkgs) lib; };

builtins.getFlake

lib-coreutils = (builtins.getFlake "git+https://gist.github.com/Malix-Labs/eec30a668f03ca4ad1060c68c3f5c442?rev=<COMMIT_HASH>").lib;

Usage

Flakes

lib-coreutils.lib.replaceGnuCoreutilsWithUutils pkgs
lib-coreutils.lib.padName "coreutils-9.5" "uutils-"

Non-flakes

lib-coreutils.replaceGnuCoreutilsWithUutils pkgs
lib-coreutils.padName "coreutils-9.5" "uutils-"
{ lib }:
let
/**
Pads the given prefix string with 'v' characters so that the final name matches the length of `targetName`.
This is typically used in conjunction with `system.replaceDependencies.replacements` where replacement dependency paths/names must be of the exact same length as the original paths/names.
# Example
```nix
padName "coreutils-8.32" "uutils-"
=> "uutils-vvvvvvv"
```
# Type
```
padName :: String -> String -> String
```
# Arguments
targetName : The target package name whose length the result must match.
prefix : The prefix to pad with 'v' characters.
*/
padName = targetName: prefix:
let
padLength = lib.stringLength targetName - lib.stringLength prefix;
in
assert lib.assertMsg (
padLength >= 0
) "Prefix '${prefix}' is longer than target name '${targetName}'";
prefix + lib.strings.replicate padLength "v";
in
{
/**
Generates a NixOS configuration fragment containing replacements to swap GNU coreutils with their Rust-based `uutils` counterparts.
The function finds coreutils dependencies in `pkgs.stdenv.initialPath` and `pkgs.coreutils-full`, constructs corresponding `uutils` packages,
and applies name padding using `padName` to satisfy the length constraint of `system.replaceDependencies.replacements`.
# Example
```nix
replaceGnuCoreutilsWithUutils pkgs
=> { system.replaceDependencies.replacements = [ ... ]; }
```
# Type
```
replaceGnuCoreutilsWithUutils :: AttrSet -> AttrSet
```
# Arguments
pkgs : The package set (nixpkgs) containing the coreutils and uutils packages.
*/
replaceGnuCoreutilsWithUutils = pkgs:
let
inherit (pkgs) lib;
# coreutils-full is the only package we care about that is not in `pkgs.stdenv.initialPath`
candidates = pkgs.stdenv.initialPath ++ [ pkgs.coreutils-full ];
oldDependencies = lib.filter (
old: lib.hasPrefix "coreutils" old.pname || lib.hasAttr "uutils-${old.pname}" pkgs
) candidates;
in
{
system.replaceDependencies.replacements = map (
oldDependency:
let
new =
if lib.hasPrefix "coreutils" oldDependency.pname then
pkgs.uutils-coreutils-noprefix
else
pkgs."uutils-${oldDependency.pname}";
prefix = lib.replaceStrings [ "utils" ] [ "uutils" ] oldDependency.pname + "-";
name = padName oldDependency.name prefix;
in
{
inherit oldDependency;
newDependency = pkgs.symlinkJoin {
inherit name;
paths = [ new ];
};
}
) oldDependencies;
};
inherit padName;
}
{
"nodes": {
"nixpkgs-lib": {
"locked": {
"lastModified": 1783217952,
"narHash": "sha256-lItVCcSNUiL9NlB0+VoZwhtZk+0jAnfjJjch1g7U0bM=",
"owner": "nix-community",
"repo": "nixpkgs.lib",
"rev": "830143f1e74a5ce8aa6cdad9c41ae84b73e8b3be",
"type": "github"
},
"original": {
"owner": "nix-community",
"repo": "nixpkgs.lib",
"type": "github"
}
},
"root": {
"inputs": {
"nixpkgs-lib": "nixpkgs-lib"
}
}
},
"root": "root",
"version": 7
}
{
description = "Lib Coreutils";
inputs.nixpkgs-lib.url = "github:nix-community/nixpkgs.lib";
outputs =
{ self, nixpkgs-lib }:
{
lib = import ./default.nix { inherit (nixpkgs-lib) lib; };
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment