liminix

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

busybox.nix (3052B)


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