1{
2 lib,
3 stdenv,
4 fetchFromRepoOrCz,
5 gnu-efi,
6 fetchpatch,
7 libuuid,
8 makeWrapper,
9 mtools,
10 nasm,
11 nixosTests,
12 perl,
13 python3,
14}:
15
16stdenv.mkDerivation {
17 pname = "syslinux";
18 version = "unstable-2019-02-07";
19
20 # This is syslinux-6.04-pre3^1; syslinux-6.04-pre3 fails to run.
21 # Same issue here https://www.syslinux.org/archives/2019-February/026330.html
22 src = fetchFromRepoOrCz {
23 repo = "syslinux";
24 rev = "b40487005223a78c3bb4c300ef6c436b3f6ec1f7";
25 hash = "sha256-XNC+X7UYxdMQQAg4MLACQLxRNnI5/ZCOiCJrEkKgPeM=";
26 };
27
28 patches =
29 let
30 archlinuxCommit = "db7884ec80642781edeead3e3bbd883a15b9b3ce";
31 fetchArchlinuxPatch =
32 name: hash:
33 fetchpatch {
34 url = "https://gitlab.archlinux.org/archlinux/packaging/packages/syslinux/-/raw/${archlinuxCommit}/${name}";
35 inherit name hash;
36 };
37 in
38 [
39 (fetchArchlinuxPatch "0002-gfxboot-menu-label.patch" "sha256-eoId3jn4eRxItil5naoGMBGOJM/p5FG74ePwxiJ0w/8=")
40 (fetchArchlinuxPatch "0005-gnu-efi-version-compatibility.patch" "sha256-5oZ/24emWNPHx621v/9i2xU6rodpVCM0R18hCU35eDk=")
41 (fetchArchlinuxPatch "0006-Replace-builtin-strlen-that-appears-to-get-optimized.patch" "sha256-aq2vKqGonhMI2gRrAYNe+VRD8Vwijn7kOOv5wqAncx8=")
42 # mbr.bin: too big (452 > 440)
43 # https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=906414
44 (fetchArchlinuxPatch "0016-strip-gnu-property.patch" "sha256-gn69YHbObqg6uTLPTtu8otNBFuCSyazwxLW3FbTNLX4=")
45 # mbr.bin: too big (452 > 440)
46 (fetchArchlinuxPatch "0017-single-load-segment.patch" "sha256-pGDcP62k50YkGgP0pFp5NSe7X0sojfLCzTmJtba2Yww=")
47 # Fixes build with "modern" gnu-efi
48 (fetchArchlinuxPatch "0018-prevent-pow-optimization.patch" "sha256-1+u8Kb6bxRbTPm0QCCt4yViLozzD/+/yspkJasOFM+g=")
49 (fetchArchlinuxPatch "0025-reproducible-build.patch" "sha256-vhth9CFHqChPQPgGhUWVpYwMFnnjoMVIGr7Wfu1jcDY=")
50 (fetchArchlinuxPatch "0027-use-correct-type-for-size.patch" "sha256-5nlKwIbXpZEyBrBSq9Zg0D+PRF7/kzEG13WzpwzDpPA=")
51
52 ./import-efisetjmp.patch
53 # Upstream patch: https://www.syslinux.org/archives/2024-February/026903.html
54 ./define-wchar_t.patch
55 # gnu-efi changed their definition to already be a 1-elem array, don't double-ref it.
56 # https://github.com/ncroxon/gnu-efi/commit/5b74db0e154ffd2fba4bcc254069844f21913988
57 ./fix-longjmp-calls.patch
58 ];
59
60 postPatch = ''
61 substituteInPlace Makefile --replace-fail /bin/pwd $(type -P pwd)
62 substituteInPlace utils/ppmtolss16 --replace-fail /usr/bin/perl $(type -P perl)
63
64 # fix tests
65 substituteInPlace tests/unittest/include/unittest/unittest.h \
66 --replace-fail /usr/include/ ""
67 '';
68
69 nativeBuildInputs = [
70 nasm
71 perl
72 python3
73 makeWrapper
74 ];
75
76 buildInputs = [
77 libuuid
78 gnu-efi
79 ];
80
81 # Fails very rarely with 'No rule to make target: ...'
82 enableParallelBuilding = false;
83
84 hardeningDisable = [
85 "pic"
86 "pie" # MBR gets too big with PIE
87 "stackprotector"
88 "fortify"
89 ];
90
91 stripDebugList = [
92 "bin"
93 "sbin"
94 "share/syslinux/com32"
95 ];
96
97 # Workaround build failure on -fno-common toolchains like upstream
98 # gcc-10. Otherwise build fails as:
99 # ld: acpi/xsdt.o:/build/syslinux-b404870/com32/gpllib/../gplinclude/memory.h:40: multiple definition of
100 # `e820_types'; memory.o:/build/syslinux-b404870/com32/gpllib/../gplinclude/memory.h:40: first defined here
101 env.NIX_CFLAGS_COMPILE = "-fcommon";
102
103 makeFlags = [
104 "BINDIR=$(out)/bin"
105 "SBINDIR=$(out)/sbin"
106 "DATADIR=$(out)/share"
107 "MANDIR=$(out)/share/man"
108 "PERL=perl"
109 "HEXDATE=0x00000000"
110 # Works around confusing (unrelated) error messages when upx is not made available
111 "UPX=false"
112
113 # Configurations needed to make use of external gnu-efi
114 "LIBEFI=${gnu-efi}/lib/libefi.a"
115 "LIBDIR=${gnu-efi}/lib/"
116 "EFIINC=${gnu-efi}/include/efi"
117
118 # Legacy bios boot target is always built
119 "bios"
120 ]
121 # Build "ia32" EFI for i686
122 ++ lib.optional stdenv.hostPlatform.isi686 "efi32"
123 # Build "x86_64" EFI for x86_64
124 ++ lib.optional stdenv.hostPlatform.isx86_64 "efi64";
125
126 # Some tests require qemu, some others fail in a sandboxed environment
127 doCheck = false;
128
129 postInstall = ''
130 wrapProgram $out/bin/syslinux \
131 --prefix PATH : "${mtools}/bin"
132
133 # Delete com32 headers to save space, nobody seems to be using them
134 rm -rf $out/share/syslinux/com32
135 '';
136
137 passthru.tests.biosCdrom = nixosTests.boot.biosCdrom;
138
139 meta = with lib; {
140 homepage = "https://www.syslinux.org/";
141 description = "Lightweight bootloader";
142 license = licenses.gpl2Plus;
143 maintainers = [ ];
144 platforms = [
145 "i686-linux"
146 "x86_64-linux"
147 ];
148 };
149}