From 2e13f1372ec1cad49ebbf40abbd03a63a6e0baa3 Mon Sep 17 00:00:00 2001
From: Daniel Barlow <dan@telent.net>
Date: Tue, 27 Sep 2022 22:07:18 +0100
Subject: [PATCH] add outputs.manifest for diagnosing image size problems

---
 README.md    | 10 ++++++++++
 THOUGHTS.txt | 10 ++++++++--
 default.nix  |  5 ++++-
 3 files changed, 22 insertions(+), 3 deletions(-)

diff --git a/README.md b/README.md
index a095fdb37..df4ab80d7 100644
--- a/README.md
+++ b/README.md
@@ -114,6 +114,16 @@ Assuming you have nixpkgs checked out in a peer directory of this one,
 
     NIX_PATH=nixpkgs=../nixpkgs:$NIX_PATH ./run-tests.sh
 
+## Diagnosing unexpectedly large images
+
+Sometimes you can add a package and it causes the image size to balloon
+because it has dependencies on other things you didn't know about. Build the
+`outputs.manifest` attribute, which is a json representation of the
+filesystem, and you can run `nix-store --query` on it:
+
+    NIX_PATH=nixpkgs=../nixpkgs:$NIX_PATH NIXPKGS_ALLOW_UNSUPPORTED_SYSTEM=1 nix-build -I liminix-config=path/to/your/configuration.nix --arg device "import ./devices/qemu.nix" -A outputs.manifest -o manifest
+    nix-store -q --tree manifest
+
 
 ## Articles of interest
 
diff --git a/THOUGHTS.txt b/THOUGHTS.txt
index 07ca690e9..b35f0444d 100644
--- a/THOUGHTS.txt
+++ b/THOUGHTS.txt
@@ -186,7 +186,7 @@ creates the interface instead of using an existing unconfigured one
 
 5) write a test for udhcp
 
-6) squashfs size is ~ 14MB for a configuration with not much in it,
+DONE 6) squashfs size is ~ 14MB for a configuration with not much in it,
 look for obvious wastes of space
 
 7) some of the pppoe config should be moved into a ppp service
@@ -207,7 +207,13 @@ is that specified or fluke?
 
 19) syslogd - use busybox or s6?
 
-20) The option currently called defaultPackages needs a better name as
+DONE 20) The option currently called defaultPackages needs a better name as
 it doesn't have the same semantics as nixos
 environment.defaultPackages.  maybe call it packagesInProfile or
 packagesOnPath. or defaultProfile.packages
+
+
+Tue Sep 27 22:00:36 BST 2022
+
+Found the cause of huge image size: rp-pppoe ships with scripts that
+reference build-time packages, so we have x86-64 glibc in there
diff --git a/default.nix b/default.nix
index 9f1696d9a..7a105478e 100644
--- a/default.nix
+++ b/default.nix
@@ -4,7 +4,7 @@
 let
   overlay = import ./overlay.nix;
   nixpkgs = import <nixpkgs> ( device.system // {overlays = [overlay]; });
-  inherit (nixpkgs.pkgs) callPackage liminix;
+  inherit (nixpkgs.pkgs) callPackage writeText liminix;
   config = (import ./merge-modules.nix) [
     ./modules/base.nix
     ({ lib, ... } : { config = { inherit (device) kernel; }; })
@@ -24,6 +24,9 @@ in {
       ln -s ${squashfs} squashfs
       ln -s ${kernel.vmlinux} vmlinux
    '';
+    # this exists so that you can run "nix-store -q --tree" on it and find
+    # out what's in the image, which is nice if it's unexpectedly huge
+    manifest = writeText "manifest.json" (builtins.toJSON config.filesystem.contents);
   };
   # this is just here as a convenience, so that we can get a
   # cross-compiling nix-shell for any package we're customizing