Skip to content

Commit

Permalink
mk: Initial support for NixOS (Linux variant)
Browse files Browse the repository at this point in the history
A particular challenge for pkgsrc on NixOS is that it usurps all
Unix conventions and stores its system binaries and libraries in
a crazy system of hashed sub-directories:

$ which ls
/run/current-system/sw/bin/ls
$ ls -l /run/current-system/sw/bin/ls
Lrwxrwxrwx 1 root root 65 Jan  1  1970 /run/current-system/sw/bin/ls -> /nix/store/xs02fpnpkq
frhqqfsxx3lpj48wrapd00-coreutils-8.32/bin/ls

We can make a "best effort" attempt to accomodate this by invoking
the compiler to figure out where libc is.  In general, it's required to
adjust the Linux files to make fewer assumptions about the layout of the
filesystem.

However, since using a compiler and libc from NixOS results in /nix/store
paths being embedded in binaries, running the NixOS "garbage collector"
can result in binaries installed from pkgsrc becoming unusable.  Use with
care:

$ readelf -a ~/pkg/bin/perl | grep nix
      [Requesting program interpreter: /nix/store/p5sam91qwz995pi0160rfr7dkh6pibil-glibc-2.32
-39/lib/ld-linux-aarch64.so.1]
 0x000000000000001d (RUNPATH)            Library runpath: [/home/nia/pkg/lib:/home/nia/pkg/li
b/perl5/5.32.0/aarch64-linux/CORE:/nix/store/p5sam91qwz995pi0160rfr7dkh6pibil-glibc-2.32-39/l
ib:/nix/store/vv9nz0bwv1pfl70w14k7dgz6yx7hjwxk-gcc-9.3.0-lib/lib]

Apparently, the "stdenv.cc" package must be installed prior to
bootstrapping pkgsrc.

I worked on this patch last year for a friend who wanted to test
something on pkgsrc but had no other system available.
  • Loading branch information
alarixnia committed May 1, 2022
1 parent 51f04f8 commit eac44bf
Show file tree
Hide file tree
Showing 3 changed files with 323 additions and 305 deletions.
4 changes: 3 additions & 1 deletion mk/bsd.prefs.mk
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# $NetBSD: bsd.prefs.mk,v 1.420 2022/04/30 21:27:57 tnn Exp $
# $NetBSD: bsd.prefs.mk,v 1.421 2022/05/01 08:03:40 nia Exp $
#
# This file includes the mk.conf file, which contains the user settings.
#
Expand Down Expand Up @@ -81,6 +81,8 @@ _CLEANING:= ${"${.TARGETS:C,( [[:alnum:]-]*clean[[:alnum:]-]*)+$,,W:M*clean*}":?
UNAME=/usr/bin/uname
.elif exists(/bin/uname)
UNAME=/bin/uname
.elif exists(/run/current-system/sw/bin/uname)
UNAME=/run/current-system/sw/bin/uname
.else
UNAME=echo Unknown
.endif
Expand Down
48 changes: 32 additions & 16 deletions mk/platform/Linux.mk
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# $NetBSD: Linux.mk,v 1.88 2022/02/10 21:22:44 schmonz Exp $
# $NetBSD: Linux.mk,v 1.89 2022/05/01 08:03:41 nia Exp $
#
# Variable definitions for the Linux operating system.

Expand All @@ -9,27 +9,36 @@ IMAKE_TOOLS= gmake # extra tools required when we use imake
.endif
IMAKEOPTS+= -DBuildHtmlManPages=NO
PKGLOCALEDIR?= share

TYPE?= type # Shell builtin

.if exists(/etc/NIXOS)
PS?= /run/current-system/sw/bin/ps
SU?= /run/current-system/sw/bin/su
USERADD?= /run/current-system/sw/bin/useradd
GROUPADD?= /run/current-system/sw/bin/groupadd
NOLOGIN?= /run/current-system/sw/bin/nologin
.endif

PS?= /bin/ps
# XXX: default from defaults/mk.conf. Verify/correct for this platform
# and remove this comment.
.if exists(/usr/bin/su)
SU?= /usr/bin/su
.else
SU?= /bin/su
.endif
TYPE?= type # Shell builtin
.if exists(/sbin/nologin)
NOLOGIN?= /sbin/nologin
.else
NOLOGIN?= /bin/false
.endif
USERADD?= /usr/sbin/useradd
GROUPADD?= /usr/sbin/groupadd

CPP_PRECOMP_FLAGS?= # unset
DEF_UMASK?= 022
DEFAULT_SERIAL_DEVICE?= /dev/null
EXPORT_SYMBOLS_LDFLAGS?= # Don't add symbols to the dynamic symbol table
GROUPADD?= /usr/sbin/groupadd
MOTIF_TYPE_DEFAULT?= motif # default 2.0 compatible libs type
.if exists(/etc/ssdlinux_version)
NOLOGIN?= /sbin/nologin
.else
NOLOGIN?= /bin/false
.endif
PKG_TOOLS_BIN?= ${LOCALBASE}/sbin
ROOT_CMD?= ${SU} - root -c
.if exists(/etc/ssdlinux_version)
Expand All @@ -43,7 +52,6 @@ ULIMIT_CMD_datasize?= ulimit -d `ulimit -H -d`
ULIMIT_CMD_stacksize?= ulimit -s `ulimit -H -s`
ULIMIT_CMD_memorysize?= ulimit -m `ulimit -H -m`
ULIMIT_CMD_cputime?= ulimit -t `ulimit -H -t`
USERADD?= /usr/sbin/useradd

_OPSYS_EMULDIR.linux= # empty
_OPSYS_EMULDIR.linux32= # empty
Expand Down Expand Up @@ -80,6 +88,11 @@ _OPSYS_LIB_DIRS?= /lib /usr/lib /lib${LIBABISUFFIX} /usr/lib${LIBABISUFFIX}
.elif exists(/etc/arch-release)
_OPSYS_SYSTEM_RPATH= /lib:/usr/lib
_OPSYS_LIB_DIRS?= /lib /usr/lib
.elif exists(/etc/NIXOS)
# NixOS has no fixed locations for system libraries.
_OPSYS_INCLUDE_DIRS!= echo "" | cpp -v 2>&1 | grep '^[[:space:]]*/.*include$$' | tr '\n' ' '

_OPSYS_LIB_DIRS!= cc -print-search-dirs | awk '/^libraries:/ { $$1=""; $$2=substr($$2, 2); print $$0; }' | tr ':' '\n'
.else
_OPSYS_SYSTEM_RPATH= /lib${LIBABISUFFIX}:/usr/lib${LIBABISUFFIX}
_OPSYS_LIB_DIRS?= /lib${LIBABISUFFIX} /usr/lib${LIBABISUFFIX}
Expand All @@ -103,11 +116,7 @@ _OPSYS_PREFER.sysexits?= native
_OPSYS_PREFER.dl?= native
_OPSYS_PREFER.pthread?= native

.if exists(/usr/include/netinet6) || exists(/usr/include/linux/in6.h)
_OPSYS_HAS_INET6= yes # IPv6 is standard
.else
_OPSYS_HAS_INET6= no # IPv6 is not standard
.endif
_OPSYS_HAS_JAVA= no # Java is not standard
_OPSYS_HAS_MANZ= no # no MANZ for gzipping of man pages
_OPSYS_HAS_OSSAUDIO= no # libossaudio is unavailable
Expand Down Expand Up @@ -184,10 +193,17 @@ _GLIBC_VERSION_CMD= if [ -x ${_glibc_path}/libc.so.6 ]; then \
ldd --version | \
sed -ne's/^ldd.* \(.*\)$$/\1/p'; \
fi
GLIBC_VERSION= ${_GLIBC_VERSION_CMD:sh}
. endif
.endfor

# In case we can't find glibc in any of the normal library paths
# (i.e. we're on NixOS), try ldconfig. Ironically, NixOS does not
# actually use ldconfig...
_GLIBC_VERSION_CMD?= ldconfig --version | \
sed -ne's/ldconfig (GNU libc) \(.*\)$$/\1/p'

GLIBC_VERSION= ${_GLIBC_VERSION_CMD:sh}

# If this is defined pass it to the make process.
.if defined(NOGCCERROR)
MAKE_ENV+= NOGCCERROR=true
Expand Down
Loading

0 comments on commit eac44bf

Please sign in to comment.