Libudev as static library?


I’m trying to build a static binary that depends on libudev.

Unfortunately nix-env -iA nixpkgs.pkgsStatic.libudev fails with:
error: p11-kit cannot be used as a static library

Is there a better way of doing this?


According to this comment, it does not make sense to build p11-kit statically. But looking at systemd expression, (libudev is an alias of systemd), the dependency is optional (systemd.override { withHomed = false; }). You can also try to use systemdMinimal, which disables most of the dependencies but still contains libudev. Edit: Actually, withHomed is disabled by default so p11-kit is likely pulled in by some dependency and it might be necessary to fix that first.

Generally, pkgsStatic, like anything, are maintained on a best-effort basis. If the build fails, the solution is to fix it in Nixpkgs and you can help with that.

Thanks, that makes things a bit clearer.

Statically building systemdMinimal (on nixpkgs-22.05pre333949.47b35f569e8) fails because of kbd:

% nix-env -iA nixpkgs.pkgsStatic.systemdMinimal
/nix/store/af96kdgvg090q1jwrndz9apx6lvnwfra-x86_64-unknown-linux-musl-binutils-2.35.2/bin/x86_64-unknown-linux-musl-ld: /nix/store/bzfz4fsayp7hjipbpylf86f13kp0lg00-linux-pam-static-x86_64-unknown-linux-musl-1.5.1/lib/libpam.a(pam_audit.o): in function `_pam_audit_writelog':
(.text+0xaa): undefined reference to `audit_log_acct_message'
/nix/store/af96kdgvg090q1jwrndz9apx6lvnwfra-x86_64-unknown-linux-musl-binutils-2.35.2/bin/x86_64-unknown-linux-musl-ld: /nix/store/bzfz4fsayp7hjipbpylf86f13kp0lg00-linux-pam-static-x86_64-unknown-linux-musl-1.5.1/lib/libpam.a(pam_audit.o): in function `_pam_audit_open':
(.text+0x175): undefined reference to `audit_open'
/nix/store/af96kdgvg090q1jwrndz9apx6lvnwfra-x86_64-unknown-linux-musl-binutils-2.35.2/bin/x86_64-unknown-linux-musl-ld: /nix/store/bzfz4fsayp7hjipbpylf86f13kp0lg00-linux-pam-static-x86_64-unknown-linux-musl-1.5.1/lib/libpam.a(pam_audit.o): in function `_pam_auditlog':
(.text+0x337): undefined reference to `audit_close'
/nix/store/af96kdgvg090q1jwrndz9apx6lvnwfra-x86_64-unknown-linux-musl-binutils-2.35.2/bin/x86_64-unknown-linux-musl-ld: /nix/store/bzfz4fsayp7hjipbpylf86f13kp0lg00-linux-pam-static-x86_64-unknown-linux-musl-1.5.1/lib/libpam.a(pam_audit.o): in function `pam_modutil_audit_write':
(.text+0x541): undefined reference to `audit_close'
error: builder for '/nix/store/7wdghkxrqgy47pfwqi8mcmgi03zsyr9y-kbd-static-x86_64-unknown-linux-musl-2.4.0.drv' failed with exit code 2
error: 1 dependencies of derivation '/nix/store/fbwz5d67kf2lrlg9lk63bdjj15q7f4gz-systemd-minimal-static-x86_64-unknown-linux-musl-249.5.drv' failed to build

The undefined reference audit_log_acct_message should be provided by the audit package, whose static version built fine:

% nm /nix/store/kasxd3y0cdn44vh7nd21nwhiaydfsq2j-audit-static-x86_64-unknown-linux-musl-2.8.5/lib/libaudit.a | grep audit_log_acct_message
0000000000000ac0 T audit_log_acct_message

audit is mentioned as a buildInput by systemd, but not by kbd. I tried adding it, but the error remains the same. Why can kbd (source) not see audit? I tried digging a bit and audit is indeed mentioned in NIX_LDFLAGS during kbd's build phase.

Please see #142472. systemd can’t be built statically because of symbol collisions, so unless we can find a resolution to that fundamental issue, none of the other failures are really relevant.

There’s also libudev-zero, which attempts to reimplement a subset of the udev API without requiring a daemon. It doesn’t currently build statically, but I’ve been working on a patch to enable that which I can prioritise since it seems like there are quite a few of us interested in static udev.

This particular issue should be fixed by #144749. It’s still in staging.

Also doesn’t pkgsStatic use musl for libc? systemd can’t be built with musl, even when dynamically linked.

It can in Nixpkgs! (And in other distros that use OpenEmbedded’s patches.)

Oh that’s new. Neat.

Oh! One more thing. Installing libraries with nix-env doesn’t work — it’s for installing programs, and even then it’s not very good at that compared to home-manager or NixOS. Libraries from Nixpkgs are only available in nix-shell and friends, or in Nix builds themselves. If you have more questions about using libraries with Nix, I recommend starting a new topic about that, where you’re more likely to reach people who can do a better job explaining than me. :slight_smile: