Skip to content

lib: add type signature to some of the functions#495873

Draft
ilkecan wants to merge 3 commits intoNixOS:masterfrom
ilkecan:lib-add-missing-type-signatures
Draft

lib: add type signature to some of the functions#495873
ilkecan wants to merge 3 commits intoNixOS:masterfrom
ilkecan:lib-add-missing-type-signatures

Conversation

@ilkecan
Copy link
Member

@ilkecan ilkecan commented Mar 2, 2026

I was looking for the list difference function using noogle and couldn't find subtractLists initially, because it lacks a type signature.

I tried to add type signatures to most of the non-module or non-internal functions, as best as I could.

Things done

  • Built on platform:
    • x86_64-linux
    • aarch64-linux
    • x86_64-darwin
    • aarch64-darwin
  • Tested, as applicable:
  • Ran nixpkgs-review on this PR. See nixpkgs-review usage.
  • Tested basic functionality of all binary files, usually in ./result/bin/.
  • Nixpkgs Release Notes
    • Package update: when the change is major or breaking.
  • NixOS Release Notes
    • Module addition: when adding a new NixOS module.
    • Module update: when the change is significant.
  • Fits CONTRIBUTING.md, pkgs/README.md, maintainers/README.md and other READMEs.

@nixpkgs-ci nixpkgs-ci bot requested review from a team, Profpatsch, alyssais, hsjobeki and infinisil March 2, 2026 15:54
@nixpkgs-ci nixpkgs-ci bot added 10.rebuild-linux: 1-10 This PR causes between 1 and 10 packages to rebuild on Linux. 10.rebuild-darwin: 1-10 This PR causes between 1 and 10 packages to rebuild on Darwin. 6.topic: lib The Nixpkgs function library labels Mar 2, 2026
@nixpkgs-ci nixpkgs-ci bot added the 12.approvals: 1 This PR was reviewed and approved by one person. label Mar 2, 2026
Copy link
Contributor

@hsjobeki hsjobeki left a comment

Choose a reason for hiding this comment

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

Thanks for engineering all the types. I know its a pain. So thanks for doing it :)

I would like to note some minor things for style consistency:
We should probably add some better styleguide if not already described in the doc/contributing

  • Section ordering: Examples before Types:
    • Examples are concrete, types are abstract: readers understand a concrete usage example faster than a type signature.
  • Type variables:
    • Concrete types start with uppercase: Int, String, Map, Bool, Derivation
    • Type variables start with lowercase: a, b, k, v, m
  • If we use Map should we also use List instead of [] ?

EDIT:

Made a PR for the ordering:

#495918

lib/attrsets.nix Outdated

```
attrsets.longestValidPathPrefix :: [String] -> Value -> [String]
longestValidPathPrefix :: [String] -> Value -> [String]
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
longestValidPathPrefix :: [String] -> Value -> [String]
longestValidPathPrefix :: [String] -> Any -> [String]


: Value to trace

# Type
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 switch the order of "Examples" vs "Type" ?

# Type
```
warnOnInstantiate :: string -> Derivation -> Derivation
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
warnOnInstantiate :: string -> Derivation -> Derivation
warnOnInstantiate :: String -> Derivation -> Derivation


```
packagesFromDirectoryRecursive :: {
callPackage :: Path -> {} -> a,
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
callPackage :: Path -> {} -> a,
callPackage :: Path -> AttrSet -> Derivation,

To make it consistent with the rest. Otherwise the {} could be interpreted as the empty set.

lib/meta.nix Outdated
# Type

```
appendToName :: string -> Derivation -> Derivation
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
appendToName :: string -> Derivation -> Derivation
appendToName :: String -> Derivation -> Derivation

Use capital String please. To avoid potential confusion with types.string

I think all named types should be capitalized for consistency.

lib/meta.nix Outdated
# Type

```
lowPrioSet :: (Map string Derivation) -> (Map string Derivation)
Copy link
Contributor

Choose a reason for hiding this comment

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

I am not a big fan of borrowing more from haskell.

In haskell:

  1. string should be String: Type names in Haskell must start with an uppercase letter. Lowercase string would be interpreted as a type variable.
  2. Parentheses are unnecessary: While not wrong, the parens around Map String Derivation are redundant here.

Idiomatic Haskell:

  lowPrioSet :: Map String Derivation -> Map String Derivation                                                         

If string is intentionally a type variable (i.e., polymorphic over the key type), then it would be valid but you'd
typically use a or k by convention:

  lowPrioSet :: Map k Derivation -> Map k Derivation                                                                   

I would look into some other files in lib first to see if there is convention in place that we can borrow instead.

lib/trivial.nix Outdated
# Type

```
and :: bool -> bool -> bool
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
and :: bool -> bool -> bool
and :: Bool -> Bool -> Bool

lib/trivial.nix Outdated
# Type

```
compare :: a -> a -> int
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
compare :: a -> a -> int
compare :: a -> a -> Int

lib/trivial.nix Outdated
# Type

```
toHexString :: int -> string
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
toHexString :: int -> string
toHexString :: Int -> String

@github-project-automation github-project-automation bot moved this to In progress in Nixpkgs Lib Mar 2, 2026
@philiptaron
Copy link
Contributor

philiptaron commented Mar 2, 2026

Holding a PR up with a style rule that wasn't there before is kind of bogus. I think this is fine to merge as-is with the select edits about the types themselves.

@ilkecan ilkecan marked this pull request as draft March 3, 2026 10:57
@ilkecan
Copy link
Member Author

ilkecan commented Mar 3, 2026

Section ordering: Examples before Types:

  • Examples are concrete, types are abstract: readers understand a concrete usage example faster than a type signature.

Majority of the existing lib functions use the ordering:

  • description
  • inputs
  • outputs
  • type
  • examples

which is why I have ordered the new type signatures that way and also "fixed" some of the outliers.

Of course current situation should not be an obstacle for possible improvements, but I think this deserves a separate discussion and existing docs should be updated afterwards depending on #495918.

Type variables:

  • Concrete types start with uppercase: Int, String, Map, Bool, Derivation
  • Type variables start with lowercase: a, b, k, v, m

Yeah, that makes sense to me.

If we use Map should we also use List instead of [] ?

I initially saw { ... } was used for attrs similar to how [ ... ] was used for list on Noogle:

partition :: (a -> bool) -> [a] -> { right :: [a]; wrong :: [a]; }
groupBy :: (a -> String) -> [ a ] -> { [String] :: [a] }

for the builtin functions.

But then saw existing Map usage instead on nixpkgs, which is why I have changed to that myself.

But upon further look, I think nixpkgs also uses { ... } for specific AttrSets and this actually seems more prevalent, so I am going to switch to that.

ilkecan added 2 commits March 3, 2026 18:30
- concrete types start with uppercase: Int, String, Bool, Derivation,
  etc.
- type variables start with lowercase: a, b, etc.
- list:
  - use `[x]` for homogeneous lists instead of `List x` or `[ x ]`
  - use `List` for heterogeneous lists (not that common in `lib`)
- attr:
  - use `AttrSet` for a generic attribute set type
  - use `{ key1 :: Type1; key2 :: Type2; ... }` adding signatures for
    known attribute names and types
  - end with an ellipsis (`...`) if the set can contain unknown
    attributes
  - use `{ [String] :: x }` if all the attributes has the same type `x`
- prefer `Any` over `a` if the latter is not reused
@nixpkgs-ci nixpkgs-ci bot added the 6.topic: module system About "NixOS" module system internals label Mar 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

6.topic: lib The Nixpkgs function library 6.topic: module system About "NixOS" module system internals 10.rebuild-darwin: 1-10 This PR causes between 1 and 10 packages to rebuild on Darwin. 10.rebuild-linux: 1-10 This PR causes between 1 and 10 packages to rebuild on Linux. 12.approvals: 1 This PR was reviewed and approved by one person.

Projects

Status: In progress

Development

Successfully merging this pull request may close these issues.

3 participants