summary refs log tree commit diff
path: root/nixos/modules/virtualisation/anbox.nix
blob: 523d9a9576ef328a10b6694e033cc8010b2a1a66 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
{ config, lib, pkgs, ... }:

with lib;

let

  cfg = config.virtualisation.anbox;

  addrOpts = v: addr: pref: name: {
    address = mkOption {
      default = addr;
      type = types.str;
      description = lib.mdDoc ''
        IPv${toString v} ${name} address.
      '';
    };

    prefixLength = mkOption {
      default = pref;
      type = types.addCheck types.int (n: n >= 0 && n <= (if v == 4 then 32 else 128));
      description = lib.mdDoc ''
        Subnet mask of the ${name} address, specified as the number of
        bits in the prefix (`${if v == 4 then "24" else "64"}`).
      '';
    };
  };

  finalImage = if cfg.imageModifications == "" then cfg.image else ( pkgs.callPackage (
    { runCommandNoCC, squashfsTools }:

    runCommandNoCC "${cfg.image.name}-modified.img" {
      nativeBuildInputs = [
        squashfsTools
      ];
    } ''
      echo "-> Extracting Anbox root image..."
      unsquashfs -dest rootfs ${cfg.image}

      echo "-> Modifying Anbox root image..."
      (
      cd rootfs
      ${cfg.imageModifications}
      )

      echo "-> Packing modified Anbox root image..."
      mksquashfs rootfs $out -comp xz -no-xattrs -all-root
    ''
  ) { });

in

{

  options.virtualisation.anbox = {

    enable = mkEnableOption (lib.mdDoc "Anbox");

    image = mkOption {
      default = pkgs.anbox.image;
      defaultText = literalExpression "pkgs.anbox.image";
      type = types.package;
      description = lib.mdDoc ''
        Base android image for Anbox.
      '';
    };

    imageModifications = mkOption {
      default = "";
      type = types.lines;
      description = lib.mdDoc ''
        Commands to edit the image filesystem.

        This can be used to e.g. bundle a privileged F-Droid.

        Commands are ran with PWD being at the root of the filesystem.
      '';
    };

    extraInit = mkOption {
      type = types.lines;
      default = "";
      description = lib.mdDoc ''
        Extra shell commands to be run inside the container image during init.
      '';
    };

    ipv4 = {
      container = addrOpts 4 "192.168.250.2" 24 "Container";
      gateway = addrOpts 4 "192.168.250.1" 24 "Host";

      dns = mkOption {
        default = "1.1.1.1";
        type = types.str;
        description = lib.mdDoc ''
          Container DNS server.
        '';
      };
    };
  };

  config = mkIf cfg.enable {

    assertions = singleton {
      assertion = with config.boot.kernelPackages; kernelAtLeast "5.5" && kernelOlder "5.18";
      message = "Anbox needs a kernel with binder and ashmem support";
    };

    environment.systemPackages = with pkgs; [ anbox ];

    systemd.mounts = singleton {
      requiredBy = [ "anbox-container-manager.service" ];
      description = "Anbox Binder File System";
      what = "binder";
      where = "/dev/binderfs";
      type = "binder";
    };

    virtualisation.lxc.enable = true;
    networking.bridges.anbox0.interfaces = [];
    networking.interfaces.anbox0.ipv4.addresses = [ cfg.ipv4.gateway ];

    networking.nat = {
      enable = true;
      internalInterfaces = [ "anbox0" ];
    };

    # Ensures NetworkManager doesn't touch anbox0
    networking.networkmanager.unmanaged = [ "anbox0" ];

    systemd.services.anbox-container-manager = let
      anboxloc = "/var/lib/anbox";
    in {
      description = "Anbox Container Management Daemon";

      environment.XDG_RUNTIME_DIR="${anboxloc}";

      wantedBy = [ "multi-user.target" ];
      preStart = let
        initsh = pkgs.writeText "nixos-init" (''
          #!/system/bin/sh
          setprop nixos.version ${config.system.nixos.version}

          # we don't have radio
          setprop ro.radio.noril yes
          stop ril-daemon

          # speed up boot
          setprop debug.sf.nobootanimation 1
        '' + cfg.extraInit);
        initshloc = "${anboxloc}/rootfs-overlay/system/etc/init.goldfish.sh";
      in ''
        mkdir -p ${anboxloc}
        mkdir -p $(dirname ${initshloc})
        [ -f ${initshloc} ] && rm ${initshloc}
        cp ${initsh} ${initshloc}
        chown 100000:100000 ${initshloc}
        chmod +x ${initshloc}
      '';

      serviceConfig = {
        ExecStart = ''
          ${pkgs.anbox}/bin/anbox container-manager \
            --data-path=${anboxloc} \
            --android-image=${finalImage} \
            --container-network-address=${cfg.ipv4.container.address} \
            --container-network-gateway=${cfg.ipv4.gateway.address} \
            --container-network-dns-servers=${cfg.ipv4.dns} \
            --use-rootfs-overlay \
            --privileged \
            --daemon
        '';
      };
    };
  };

}