12 cfg = config.virtualisation.anbox;
14 addrOpts = v: addr: pref: name: {
19 IPv${toString v} ${name} address.
23 prefixLength = mkOption {
25 type = types.addCheck types.int (n: n >= 0 && n <= (if v == 4 then 32 else 128));
27 Subnet mask of the ${name} address, specified as the number of
28 bits in the prefix (`${if v == 4 then "24" else "64"}`).
34 if cfg.imageModifications == "" then
38 { runCommandNoCC, squashfsTools }:
40 runCommandNoCC "${cfg.image.name}-modified.img"
47 echo "-> Extracting Anbox root image..."
48 unsquashfs -dest rootfs ${cfg.image}
50 echo "-> Modifying Anbox root image..."
53 ${cfg.imageModifications}
56 echo "-> Packing modified Anbox root image..."
57 mksquashfs rootfs $out -comp xz -no-xattrs -all-root
65 options.virtualisation.anbox = {
67 enable = mkEnableOption "Anbox";
70 default = pkgs.anbox.image;
71 defaultText = literalExpression "pkgs.anbox.image";
74 Base android image for Anbox.
78 imageModifications = mkOption {
82 Commands to edit the image filesystem.
84 This can be used to e.g. bundle a privileged F-Droid.
86 Commands are ran with PWD being at the root of the filesystem.
90 extraInit = mkOption {
94 Extra shell commands to be run inside the container image during init.
99 container = addrOpts 4 "192.168.250.2" 24 "Container";
100 gateway = addrOpts 4 "192.168.250.1" 24 "Host";
106 Container DNS server.
112 config = mkIf cfg.enable {
114 assertions = singleton {
115 assertion = with config.boot.kernelPackages; kernelAtLeast "5.5" && kernelOlder "5.18";
116 message = "Anbox needs a kernel with binder and ashmem support";
119 environment.systemPackages = with pkgs; [ anbox ];
121 systemd.mounts = singleton {
122 requiredBy = [ "anbox-container-manager.service" ];
123 description = "Anbox Binder File System";
125 where = "/dev/binderfs";
129 virtualisation.lxc.enable = true;
130 networking.bridges.anbox0.interfaces = [ ];
131 networking.interfaces.anbox0.ipv4.addresses = [ cfg.ipv4.gateway ];
135 internalInterfaces = [ "anbox0" ];
138 # Ensures NetworkManager doesn't touch anbox0
139 networking.networkmanager.unmanaged = [ "anbox0" ];
141 systemd.services.anbox-container-manager =
143 anboxloc = "/var/lib/anbox";
146 description = "Anbox Container Management Daemon";
148 environment.XDG_RUNTIME_DIR = "${anboxloc}";
150 wantedBy = [ "multi-user.target" ];
153 initsh = pkgs.writeText "nixos-init" (
156 setprop nixos.version ${config.system.nixos.version}
158 # we don't have radio
159 setprop ro.radio.noril yes
163 setprop debug.sf.nobootanimation 1
167 initshloc = "${anboxloc}/rootfs-overlay/system/etc/init.goldfish.sh";
171 mkdir -p $(dirname ${initshloc})
172 [ -f ${initshloc} ] && rm ${initshloc}
173 cp ${initsh} ${initshloc}
174 chown 100000:100000 ${initshloc}
175 chmod +x ${initshloc}
180 ${pkgs.anbox}/bin/anbox container-manager \
181 --data-path=${anboxloc} \
182 --android-image=${finalImage} \
183 --container-network-address=${cfg.ipv4.container.address} \
184 --container-network-gateway=${cfg.ipv4.gateway.address} \
185 --container-network-dns-servers=${cfg.ipv4.dns} \
186 --use-rootfs-overlay \