liminix

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

busybox.nix (3494B)


      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 { lib, pkgs, config, ...}:
     10 let
     11   inherit (lib) mkOption mkEnableOption types mapAttrsToList;
     12   inherit (pkgs.pseudofile) dir symlink;
     13   inherit (lib.strings) toUpper;
     14 
     15   attrs = { options, applets } :
     16     let
     17       extraOptions = builtins.concatStringsSep "\n"
     18         (mapAttrsToList (n: v: "CONFIG_${toUpper n} ${toString v}") options);
     19       appletOptions = builtins.concatStringsSep "\n"
     20         (map (n: "CONFIG_${toUpper n} y") applets);
     21     in {
     22       enableMinimal = true;
     23       extraConfig = ''
     24         ${extraOptions}
     25         ${appletOptions}
     26       '';
     27     };
     28   cfg = config.programs.busybox;
     29   busybox = pkgs.busybox.override (attrs { inherit (cfg) applets options; });
     30   makeLinks = lib.attrsets.genAttrs
     31     cfg.applets
     32     (a: symlink "${busybox}/bin/busybox");
     33   minimalApplets = [
     34     # this is probably less minimal than it could be
     35     "arch" "ash" "base64" "basename" "bc" "brctl" "bunzip2" "bzcat"
     36     "bzip2" "cal" "cat" "chattr" "chgrp" "chmod" "chown" "chpst"
     37     "chroot" "clear" "cmp" "comm" "cp" "cpio" "cut" "date" "dd" "df"
     38     "dirname" "dmesg" "du" "echo" "egrep" "env" "expand" "expr"
     39     "false" "fdisk" "fgrep" "find" "free" "fuser" "grep" "gunzip"
     40     "gzip" "head" "hexdump" "hostname" "hwclock" "ifconfig" "ip"
     41     "ipaddr" "iplink" "ipneigh" "iproute" "iprule" "kill" "killall"
     42     "killall5" "less" "ln" "ls" "lsattr" "lsof" "md5sum" "mkdir"
     43     "mknod" "mktemp" "mount" "mv" "nc" "netstat" "nohup" "od" "pgrep"
     44     "pidof" "ping" "ping6" "pkill" "pmap" "printenv" "printf" "ps"
     45     "pwd" "readlink" "realpath" "reset" "rm" "rmdir" "route" "sed"
     46     "seq" "setsid" "sha1sum" "sha256sum" "sha512sum" "sleep" "sort"
     47     "stat" "strings" "stty" "su" "sum" "swapoff" "swapon" "sync"
     48     "tail" "tee" "test" "time" "touch" "tr" "traceroute" "traceroute6"
     49     "true" "truncate" "tty" "udhcpc" "umount" "uname"
     50     "unexpand" "uniq" "unlink" "unlzma" "unxz" "unzip" "uptime"
     51     "watch" "wc" "whoami" "xargs" "xxd" "xz" "xzcat" "yes" "zcat"
     52   ];
     53 in {
     54   options = {
     55     programs.busybox = {
     56       applets =  mkOption {
     57         type = types.listOf types.str;
     58         description = "Applets required";
     59         default = [];
     60         example = ["sh" "getty" "login"];
     61       };
     62       options = mkOption {
     63         # mostly the values are y n or m, but sometimes
     64         # other strings are also used
     65         description = "Other busybox config flags that do not map directly to applet names (often prefixed FEATURE_)";
     66         type = types.attrsOf types.nonEmptyStr;
     67         default =  { };
     68         example =  { FEATURE_DD_IBS_OBS = "y"; };
     69       };
     70     };
     71   };
     72   config = {
     73     programs.busybox = {
     74       applets = minimalApplets;
     75       options = {
     76         ASH_ECHO = "y";
     77         # ASH_OPTIMIZE_FOR_SIZE = "y";
     78         BASH_IS_NONE = "y";
     79         SH_IS_ASH = "y";
     80         ASH_BASH_COMPAT = "y";
     81         FEATURE_EDITING = "y"; # readline-ish command editing
     82         FEATURE_EDITING_HISTORY = "128";
     83         FEATURE_EDITING_MAX_LEN = "1024";
     84         FEATURE_TAB_COMPLETION = "y";
     85         FEATURE_EDITING_WINCH = "y";
     86         FEATURE_IPV6 = "y";
     87       };
     88     };
     89     filesystem = dir {
     90       bin = dir ({
     91         busybox = symlink "${busybox}/bin/busybox";
     92         sh = symlink "${busybox}/bin/busybox";
     93       } // makeLinks);
     94     };
     95   };
     96 }