Skip to content

Clean up Base* code#13693

Merged
Ericson2314 merged 1 commit intoNixOS:masterfrom
obsidiansystems:more-base-files
Aug 6, 2025
Merged

Clean up Base* code#13693
Ericson2314 merged 1 commit intoNixOS:masterfrom
obsidiansystems:more-base-files

Conversation

@Ericson2314
Copy link
Member

Motivation

Make it separate from Hash, since other things can be base-encoded too.

This isn't really needed for Nix, but it makes the code easier to read e.g. for someone reimplementing this stuff in a different language. (Of course, Base16/Base64 should be gotten off-the-shelf, but now the hash code, which is more bespoke, is less cluttered with the parts that would be from some library.)

Many reimplementations of "Nix32" and our hash type already exist, so this cleanup is coming years too late, but I say better late than never / it is always good to nudge the code in the direction of being a "living spec".

Context


Add 👍 to pull requests you find important.

The Nix maintainer team uses a GitHub project board to schedule and track reviews.

@Ericson2314 Ericson2314 requested a review from edolstra as a code owner August 5, 2025 18:19
@github-actions github-actions bot added the fetching Networking with the outside (non-Nix) world, input locking label Aug 5, 2025
@Mic92 Mic92 requested a review from Copilot August 5, 2025 19:03
@Mic92
Copy link
Member

Mic92 commented Aug 5, 2025

git-add?

       > In file included from ../ref-scan-bench.cc:3:
       > /nix/store/jbwx3hmahj4zihpdqxjmvdilsw12lv0b-nix-util-2.31.0pre-dev/include/nix/util/base-nix-32.hh:10:10: fatal error: 'nix/util/array-from-string-literal.hh' file not found
       >    10 | #include "nix/util/array-from-string-literal.hh"
       >       |          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
       > 1 error generated.

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR refactors base encoding functionality by extracting Base16/Base32/Base64 implementations from the hash module into dedicated base-n modules. The goal is to make the hash code less cluttered and create more reusable encoding utilities that can be used independently of hashing.

Key changes include:

  • Extracted base64 encoding/decoding from util.cc into new base-n.cc module
  • Moved base16 encoding logic from hash module to base-n.cc
  • Enhanced base-nix-32 module with decode functionality
  • Updated all call sites to use new namespaced functions (base64::encode, base16::encode, etc.)

Reviewed Changes

Copilot reviewed 18 out of 18 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
src/libutil/util.cc Removed base64 encoding functions (moved to base-n.cc)
src/libutil/base-n.cc New file containing base16 and base64 encoding/decoding implementations
src/libutil/include/nix/util/base-n.hh New header defining base16 and base64 namespaces and function signatures
src/libutil/include/nix/util/array-from-string-literal.hh New utility for converting string literals to arrays without null terminator
src/libutil/base-nix-32.cc Added decode function and updated to use std::byte
src/libutil/include/nix/util/base-nix-32.hh Updated signatures and added decode function declaration
src/libutil/hash.cc Refactored to use new base-n functions and simplified hash parsing logic
src/libutil/include/nix/util/hash.hh Removed base encoding length calculation methods
Multiple files Updated includes and function calls to use new base-n namespaces


TEST(base64Decode, decodeThrowsOnInvalidChar)
{
ASSERT_THROW(base64::decode("cXVvZCBlcm_0IGRlbW9uc3RyYW5kdW0="), Error);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you also add a test where there are extra trailing padding = characters at the end? Both snix/lix ran into the fact that parser isn't entirely correct in that regard and nixpkgs relies on some of the broken behaviour.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Ericson2314 Ericson2314 force-pushed the more-base-files branch 2 times, most recently from 2e88419 to 0b56d79 Compare August 5, 2025 22:21
@github-actions github-actions bot added the new-cli Relating to the "nix" command label Aug 5, 2025
@Ericson2314
Copy link
Member Author

OK I think this is ready.

Make it separate from Hash, since other things can be base-encoded too.

This isn't really needed for Nix, but it makes the code easier to read
e.g. for someone reimplementing this stuff in a different language. (Of
course, Base16/Base64 should be gotten off-the-shelf, but now the hash
code, which is more bespoke, is less cluttered with the parts that would
be from some library.)

Many reimplementations of "Nix32" and our hash type already exist, so
this cleanup is coming years too late, but I say better late than never
/ it is always good to nudge the code in the direction of being a
"living spec".

Co-authored-by: Sergei Zimmerman <sergei@zimmerman.foo>
@Ericson2314
Copy link
Member Author

OK, think this is ready now?

Copy link
Contributor

@xokdvium xokdvium left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed with --color-moved. Most of the diff is just moving code around.

@Ericson2314 Ericson2314 merged commit e25ab02 into NixOS:master Aug 6, 2025
14 checks passed
@Ericson2314 Ericson2314 deleted the more-base-files branch August 6, 2025 17:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

fetching Networking with the outside (non-Nix) world, input locking new-cli Relating to the "nix" command

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants