busybox.nix (4063B)
1 ## Busybox 2 ## ======= 3 ## 4 ## Busybox provides stripped-down versions of many usual 5 ## Linux/Unix tools, and may be configured to include only 6 ## the commands (termed "applets") required by the user or 7 ## by other included modules. 8 9 { 10 lib, 11 pkgs, 12 config, 13 ... 14 }: 15 let 16 inherit (lib) mkOption types mapAttrsToList; 17 inherit (pkgs.pseudofile) dir symlink; 18 inherit (lib.strings) toUpper; 19 20 attrs = 21 { options, applets }: 22 let 23 extraOptions = builtins.concatStringsSep "\n" ( 24 mapAttrsToList (n: v: "CONFIG_${toUpper n} ${toString v}") options 25 ); 26 appletOptions = builtins.concatStringsSep "\n" (map (n: "CONFIG_${toUpper n} y") applets); 27 in 28 { 29 enableMinimal = true; 30 extraConfig = '' 31 ${extraOptions} 32 ${appletOptions} 33 ''; 34 }; 35 cfg = config.programs.busybox; 36 busybox = pkgs.busybox.override (attrs { 37 inherit (cfg) applets options; 38 }); 39 makeLinks = lib.attrsets.genAttrs cfg.applets (a: symlink "${busybox}/bin/busybox"); 40 minimalApplets = [ 41 # this is probably less minimal than it could be 42 "arch" 43 "ash" 44 "base64" 45 "basename" 46 "bc" 47 "brctl" 48 "bunzip2" 49 "bzcat" 50 "bzip2" 51 "cal" 52 "cat" 53 "chattr" 54 "chgrp" 55 "chmod" 56 "chown" 57 "chpst" 58 "chroot" 59 "clear" 60 "cmp" 61 "comm" 62 "cp" 63 "cpio" 64 "cut" 65 "date" 66 "dhcprelay" 67 "dd" 68 "df" 69 "dirname" 70 "dmesg" 71 "du" 72 "echo" 73 "egrep" 74 "env" 75 "expand" 76 "expr" 77 "false" 78 "fdisk" 79 "fgrep" 80 "find" 81 "free" 82 "fuser" 83 "grep" 84 "gunzip" 85 "gzip" 86 "head" 87 "hexdump" 88 "hostname" 89 "hwclock" 90 "ifconfig" 91 "ip" 92 "ipaddr" 93 "iplink" 94 "ipneigh" 95 "iproute" 96 "iprule" 97 "kill" 98 "killall" 99 "killall5" 100 "less" 101 "ln" 102 "ls" 103 "lsattr" 104 "lsof" 105 "md5sum" 106 "mkdir" 107 "mknod" 108 "mktemp" 109 "mount" 110 "mv" 111 "nc" 112 "netstat" 113 "nohup" 114 "od" 115 "pgrep" 116 "pidof" 117 "ping" 118 "ping6" 119 "pkill" 120 "pmap" 121 "printenv" 122 "printf" 123 "ps" 124 "pwd" 125 "readlink" 126 "realpath" 127 "reset" 128 "rm" 129 "rmdir" 130 "route" 131 "sed" 132 "seq" 133 "setsid" 134 "sha1sum" 135 "sha256sum" 136 "sha512sum" 137 "sleep" 138 "sort" 139 "stat" 140 "strings" 141 "stty" 142 "su" 143 "sum" 144 "swapoff" 145 "swapon" 146 "sync" 147 "tail" 148 "tee" 149 "test" 150 "time" 151 "touch" 152 "tr" 153 "traceroute" 154 "traceroute6" 155 "true" 156 "truncate" 157 "tty" 158 "udhcpc" 159 "umount" 160 "uname" 161 "unexpand" 162 "uniq" 163 "unlink" 164 "unlzma" 165 "unxz" 166 "unzip" 167 "uptime" 168 "watch" 169 "wc" 170 "whoami" 171 "xargs" 172 "xxd" 173 "xz" 174 "xzcat" 175 "yes" 176 "zcat" 177 ]; 178 in 179 { 180 options = { 181 programs.busybox = { 182 applets = mkOption { 183 type = types.listOf types.str; 184 description = "Applets required"; 185 default = [ ]; 186 example = [ 187 "sh" 188 "getty" 189 "login" 190 ]; 191 }; 192 options = mkOption { 193 # mostly the values are y n or m, but sometimes 194 # other strings are also used 195 description = "Other busybox config flags that do not map directly to applet names (often prefixed FEATURE_)"; 196 type = types.attrsOf types.nonEmptyStr; 197 default = { }; 198 example = { 199 FEATURE_DD_IBS_OBS = "y"; 200 }; 201 }; 202 }; 203 }; 204 config = { 205 programs.busybox = { 206 applets = minimalApplets; 207 options = { 208 ASH_ECHO = "y"; 209 # ASH_OPTIMIZE_FOR_SIZE = "y"; 210 BASH_IS_NONE = "y"; 211 SH_IS_ASH = "y"; 212 ASH_BASH_COMPAT = "y"; 213 FEATURE_EDITING = "y"; # readline-ish command editing 214 FEATURE_EDITING_HISTORY = "128"; 215 FEATURE_EDITING_MAX_LEN = "1024"; 216 FEATURE_TAB_COMPLETION = "y"; 217 FEATURE_EDITING_WINCH = "y"; 218 FEATURE_IPV6 = "y"; 219 }; 220 }; 221 filesystem = dir { 222 bin = dir ( 223 { 224 busybox = symlink "${busybox}/bin/busybox"; 225 sh = symlink "${busybox}/bin/busybox"; 226 } 227 // makeLinks 228 ); 229 }; 230 }; 231 }