From 139caccebbbea462b8c68aaee9a8f1daa883abc6 Mon Sep 17 00:00:00 2001
From: Tom Hubrecht <tom.hubrecht@dgnum.eu>
Date: Thu, 12 Dec 2024 10:45:46 +0100
Subject: [PATCH] feat(nix-reuse): Add downloadLicenses option

When set to true, entering the shell will download all missing licenses
from the project
---
 README.md         |  1 +
 default.nix       |  2 ++
 modules/reuse.nix | 13 +++++++++++++
 3 files changed, 16 insertions(+)

diff --git a/README.md b/README.md
index 1610dcc..a4f4ad9 100644
--- a/README.md
+++ b/README.md
@@ -62,6 +62,7 @@ mkShell {
 
 - `defaultLicense`: the default license SPDX identifier used accross the project.
 - `defaultCopyright`: the default copyright text used accross the project.
+- `downloadLicenses`: whether to download automatically the missing licenses detectd in the project.
 - `annotations`: The list of [annotations](https://reuse.software/spec-3.3/#reusetoml) for this project
 
 ### reuse git-hook
diff --git a/default.nix b/default.nix
index afa1563..2804a7f 100644
--- a/default.nix
+++ b/default.nix
@@ -65,6 +65,8 @@ let
     defaultLicense = "EUPL-1.2";
     defaultCopyright = "2024 Tom Hubrecht <tom.hubrecht@dgnum.eu>";
 
+    downloadLicenses = true;
+
     annotations = [
       # Basic paths
       {
diff --git a/modules/reuse.nix b/modules/reuse.nix
index 3597667..f8badb8 100644
--- a/modules/reuse.nix
+++ b/modules/reuse.nix
@@ -12,15 +12,18 @@
 let
   inherit (lib)
     filterAttrs
+    getExe
     getExe'
     mapAttrs'
     mkDefault
     mkOption
     nameValuePair
+    optionalString
     ;
 
   inherit (lib.types)
     attrsOf
+    bool
     either
     enum
     listOf
@@ -76,6 +79,14 @@ in
       '';
     };
 
+    downloadLicenses = mkOption {
+      type = bool;
+      default = false;
+      description = ''
+        Try to download all licenses present in the configuration.
+      '';
+    };
+
     annotations = mkOption {
       type = listOf (
         submodule (
@@ -175,6 +186,8 @@ in
             cp --no-preserve=mode,ownership ${result} "$GIT_WC/REUSE.toml" && echo "nix-reuse: Updated REUSE.toml"
           fi
         fi
+
+        ${optionalString config.downloadLicenses "${getExe pkgs.reuse} download --all"}
       '';
   };
 }