Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose namespaced lib on pkgs #155

Closed

Conversation

blaggacao
Copy link
Contributor

I'm not 100% sure if the local library is already supercharged on lib, but also for sharing as an overlay I think a name spaced identifier is a better solution in any case.

Currently, I adopted myLib, but that should probably become myname.lib so that I can use library functions more cleanly from fellow devossers. Though, I didn't want to propose that together with this PR, since it is an independent change / discussion.

@nrdxp
Copy link
Collaborator

nrdxp commented Mar 10, 2021

I'm not clear on the benefit here. Can you perhaps give a good example?

@blaggacao
Copy link
Contributor Author

blaggacao commented Mar 10, 2021

I'm not clear on the benefit here. Can you perhaps give a good example?

Hm, ok. I'll try to explain myself better:

  • I am assuming that pkgs.lib does not carry the custom library functions of ./lib (I might be erred on that already).
  • Then I write a custom library function that I want to use in my environment.
  • Then I want to use that custom function in ./pkgs or ./modules
  • My best answer to "How do I get the custom function there?" was "Expose it on pkgs", but using pkgs.lib would have been tinkering with the global name space, which I saw as risky and prone of eventual future naming conflicts. Hence, I figured pkgs.myLib would solve this partially. Partially only, because if everyone name spaces with pkgs.myLib and we start sharing library functions, then we end up with name conflicts again. A better solution would have been pkgs.baggacao.lib but this would have been too much of a change for this PR as it requires passing the user name arroud and I think needs proper evaluation separately.
show me the code
commit af904bd2818c7f30906a1ed91695feb9cbf0300c
Author: David Arnold <[email protected]>
Date:   Mon Mar 1 15:13:28 2021 -0500

    readd: plut packages and library functions

diff --git a/lib/default.nix b/lib/default.nix
index 74e74cf..4812bbd 100644
--- a/lib/default.nix
+++ b/lib/default.nix
@@ -229,4 +229,7 @@ in
       })
       { }
       packagesNames;
+
+  # Utilities
+  mkPlutoApp = pkgs.callPackage ./nixos-lib/mkPlutoApp { };
 }
diff --git a/lib/nixos-lib/mkPlutoApp/default.nix b/lib/nixos-lib/mkPlutoApp/default.nix
new file mode 100644
index 0000000..84616ef
--- /dev/null
+++ b/lib/nixos-lib/mkPlutoApp/default.nix
@@ -0,0 +1,15 @@
+{ pkgs, writers, ... }:
+{ name, cmd, workspace, baseKey }:
+let
+  isWorkspaceEmpty = writers.writePython3 "is-workspace-empty" {
+    libraries = [ pkgs.python3Packages.i3ipc ];
+  } (builtins.readFile ./is-workspace-empty.py);
+
+  ws = builtins.toString workspace;
+in
+''
+
+  # ${name}
+  #bindsym ${baseKey}+${ws} workspace ${ws}; exec ${cmd}
+  bindsym ${baseKey}+${ws} workspace ${ws}; exec bash -c "${isWorkspaceEmpty} && ${cmd}"
+''
diff --git a/lib/nixos-lib/mkPlutoApp/is-workspace-empty.py b/lib/nixos-lib/mkPlutoApp/is-workspace-empty.py
new file mode 100644
index 0000000..915c88a
--- /dev/null
+++ b/lib/nixos-lib/mkPlutoApp/is-workspace-empty.py
@@ -0,0 +1,16 @@
+# returns 0/1 if current workspace is empty/non-empty
+
+import i3ipc
+
+i3 = i3ipc.Connection()
+tree = i3.get_tree()
+
+
+def current_workspace():
+    return tree.find_focused().workspace()
+
+
+if current_workspace().leaves():
+    print("Error current workspace is not empty")
+    exit(1)
+exit(0)

Note: myLib.mkPlutoApp

# /modules/pluto/programs/ethercalc.nix
{ config, lib, myLib, pkgs, ... }:
with lib;
let
  cfg = config.pluto.programs.${name};
  name = "ethercalc";
  url = "https://ethercalc.org/";
in
{

  ##### interface

  options.pluto.programs.${name} = {
    enable = mkEnableOption name;
    workspace = mkOption {
      type = with types; nullOr int;
    };
  };

  ##### implementation

  config.services.xserver.windowManager.i3.extraConfig = mkIf cfg.enable [
    (
      myLib.mkPlutoApp {
        inherit name;
        cmd = "${pkgs.surf}/bin/surf ${url}";
        workspace = cfg.workspace;
        baseKey = config.pluto.keymap.open;
      }
    )
  ];
}

@nrdxp
Copy link
Collaborator

nrdxp commented Mar 13, 2021

Are you aware that lib is already available via lib.flk? After #157 is merged, it will be lib.dev.

@blaggacao
Copy link
Contributor Author

Ah ok, I was already mislead on the first point. 👍

@blaggacao blaggacao closed this Mar 13, 2021
@blaggacao blaggacao deleted the da/expose-namespaced-lib-on-pkgs branch March 14, 2021 14:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants