From 25e16fed7a9a52614fc1a3ec6a4def6949b7ab47 Mon Sep 17 00:00:00 2001
From: lois <lois@procolix.eu>
Date: Tue, 11 Mar 2025 14:46:11 +0100
Subject: [PATCH] Convert configuration form to json WIP: Get nix in scope

---
 panel/default.nix           |  1 +
 panel/nix/configuration.nix |  1 +
 panel/nix/package.nix       |  2 ++
 panel/src/panel/views.py    | 17 +++++++++++------
 4 files changed, 15 insertions(+), 6 deletions(-)

diff --git a/panel/default.nix b/panel/default.nix
index dc8e81ef..0246337f 100644
--- a/panel/default.nix
+++ b/panel/default.nix
@@ -22,6 +22,7 @@ in
     packages = [
       pkgs.npins
       manage
+      pkgs.nix
     ];
     env = {
       NPINS_DIRECTORY = toString ../npins;
diff --git a/panel/nix/configuration.nix b/panel/nix/configuration.nix
index d790e30c..7e6cb437 100644
--- a/panel/nix/configuration.nix
+++ b/panel/nix/configuration.nix
@@ -37,6 +37,7 @@ let
       django-libsass
       django_4
       setuptools
+      pkgs.nix
     ]
     ++ cfg.package.propagatedBuildInputs
   );
diff --git a/panel/nix/package.nix b/panel/nix/package.nix
index e6196686..c43d850c 100644
--- a/panel/nix/package.nix
+++ b/panel/nix/package.nix
@@ -2,6 +2,7 @@
   lib,
   sqlite,
   python3,
+  nix,
 }:
 let
   src =
@@ -50,6 +51,7 @@ python3.pkgs.buildPythonPackage {
     in
     [
       sqlite
+      nix
     ]
     ++ pythonPackages;
 
diff --git a/panel/src/panel/views.py b/panel/src/panel/views.py
index dbda943f..17a0a686 100644
--- a/panel/src/panel/views.py
+++ b/panel/src/panel/views.py
@@ -11,18 +11,23 @@ from django.views.generic.edit import FormView
 from panel import models
 from panel.configuration import Version
 
+
 class Index(TemplateView):
     template_name = 'index.html'
 
+
 class AccountDetail(LoginRequiredMixin, DetailView):
     model = User
     template_name = 'account_detail.html'
+
     def get_object(self):
         return self.request.user
 
+
 class ServiceList(TemplateView):
     template_name = 'service_list.html'
 
+
 class ConfigurationForm(LoginRequiredMixin, FormView):
     template_name = 'configuration_form.html'
     success_url = reverse_lazy('configuration_form')
@@ -42,14 +47,14 @@ class ConfigurationForm(LoginRequiredMixin, FormView):
             operator=self.request.user,
         )
 
-        button_name = self.request.POST.get('save_draft') or self.request.POST.get('publish')
-        print(f"self.request.POST: {self.request.POST}")
-        print(f"button_name: {button_name}")
-        if button_name == 'deploy':
+        # Check for deploy button
+        if "deploy" in self.request.POST.keys():
             print("DEPLOYING:")
             print(os.getenv("REPO_DIR"))
-            print(obj)
-            subprocess.run(["nix", "develop", "--command", "nixops4", "apply", "test"], cwd=os.getenv("REPO_DIR"), env={"DEPLOYMENT": obj})
+            config_dict = obj.parsed_value.model_dump_json()
+            print(f"config_dict: {config_dict}")
+            subprocess.run(["nix", "develop", "--command", "nixops4", "apply",
+                           "test"], cwd=os.getenv("REPO_DIR"), env={"DEPLOYMENT": config_dict})
         return obj
 
     # TODO(@fricklerhandwerk):