niten  
                
               
                 
              
                  
                    February 18, 2021,  6:42pm
                   
                   
              1 
               
             
            
              Over the process of building my configuration for several different hosts, and now fooling with NixOps, I’ve built up a collection of helpful utility functions.
The problem is, I haven’t found a good way to make them accessible in any sort of global way. I’d like  to be able to add them to lib, say in lib.<my-namespace>.someUtilFunc. That doesn’t seem to be possible…any kind of assignment to lib fails.
I end up just doing:
let
  my-lib = import ../path/to/my-lib.nix
in { ... }
 
…in every file where I need the utils. That’s kind of annoying, and seems clumsy. Or, I just end up rewriting the same functions in several different places.
Is there a way to do what I’d like and just add them to lib somehow?
(It just occurred to me that I could import my-lib early on, and pass it around as a sister arg to lib…that also seems a bit awkward, though).
             
            
               
               
              3 Likes 
            
           
          
            
              
                NobbZ  
                
               
              
                  
                    February 18, 2021,  7:37pm
                   
                   
              2 
               
             
            
              Use an overlay that does lib = super.lib // { my-namespace = import funcs.nix; }
             
            
               
               
              3 Likes 
            
           
          
            
            
              I’m not personally familiar with the nuance of its usage, but you might also look at existing uses of extends and makeExtensible within nixpkgs:
I’m only passingly aware of it after seeing Henrik’s usage here:
             
            
               
               
               
            
           
          
            
              
                niten  
                
               
              
                  
                    February 18, 2021, 10:09pm
                   
                   
              4 
               
             
            
              Perfect, that helps! I’ll give it a shot. Thanks!
             
            
               
               
               
            
           
          
            
              
                pinpox  
                
               
              
                  
                    July 27, 2021,  7:02am
                   
                   
              5 
               
             
            
              
I tried putting this in my overlay, but I get an error:
# overlays/default.nix
 lib = super.lib // { pinpox = import ../utils; };
 
# utils/default.nix
{ pkgs, ... }:
{
  renderMustache = name: template: data:
    pkgs.stdenv.mkDerivation {
      name = "${name}";
      nativeBuildInpts = [ pkgs.mustache-go ];
      passAsFile = [ "jsonData" ];
      jsonData = builtins.toJSON data;
      phases = [ "buildPhase" "installPhase" ];
      buildPhase = ''
        ${pkgs.mustache-go}/bin/mustache $jsonDataPath ${template} > rendered_file
      '';
      installPhase = ''
        cp rendered_file $out
      '';
    };
  }
 
error: attribute 'pinpox' missing
       at /nix/store/kr41f702jaw14vzqz8q5pk5d7qaw8nj5-source/modules/default-desktop/default.nix:156:18:
          155|
          156|           text = lib.pinpox.renderMustache "nixcolors.lua" ./nixcolors.lua.mustache vars.colors;
             |                  ^
          157|
 
Could you clarify how to do this? How do I access my functions?
             
            
               
               
              1 Like 
            
           
          
            
              
                NobbZ  
                
               
              
                  
                    July 27, 2021,  7:16am
                   
                   
              6 
               
             
            
              How do you use your overlays/default nix and what is its full content?
             
            
               
               
               
            
           
          
            
              
                pinpox  
                
               
              
                  
                    July 27, 2021,  7:31am
                   
                   
              7 
               
             
            
              I pushed my current state to a new brach to clarify.
This is my overlay
  
  
    
      inputs:
let
  # Pass flake inputs to overlay so we can use the sources pinned in flake.lock
  # instead of having to keep sha256 hashes in each package for src
  inherit inputs;
in self: super: {
  # Example package, used only for tests
  hello-custom = super.callPackage ../packages/hello-custom { };
  # Custom packages. Will be made available on all machines and used where
  # needed.
  wezterm-bin = super.callPackage ../packages/wezterm-bin { };
  wezterm-nightly = super.callPackage ../packages/wezterm-nightly { };
  filebrowser = super.callPackage ../packages/filebrowser { };
  zk = super.callPackage ../packages/zk { };
  # Vim plugins, added inside existing pkgs.vimPlugins
  vimPlugins = super.vimPlugins // {
    indent-blankline-nvim-lua =
      super.callPackage ../packages/indent-blankline-nvim-lua {
 
  This file has been truncated. show original 
   
  
    
    
  
  
 
I’m trying to add this function to lib, if possible with a custom namespace:
  
  
    
    
      
          { pkgs, ... }: 
          { 
            renderMustache = name: template: data: 
              # Render handlebars `template` called `name` by converting `data` to JSON 
              pkgs.stdenv.mkDerivation { 
           
                name = "${name}"; 
           
                # Disable phases which are not needed. In particular the unpackPhase will 
                # fail, if no src attribute is set 
                nativeBuildInpts = [ pkgs.mustache-go ]; 
           
                # Pass Json as file to avoid escaping 
       
     
   
  
    
    
  
  
 
To test it, I’m trying  to call it here but get the error pasted above:
  
  
    
    
      
          services.gvfs = { 
            enable = true; 
            # Default package does not support all protocols. Use the full-featured 
            # gnome version 
            package = lib.mkForce pkgs.gnome3.gvfs; 
          }; 
           
          environment.etc = { 
            testfile = { 
              source = 
                lib.pinpox.renderMustache "testfile" ./test.mustache {}; 
              mode = "0440"; 
            }; 
          }; 
           
          # This value determines the NixOS release from which the default 
          # settings for stateful data, like file locations and database versions 
          # on your system were taken. It‘s perfectly fine and recommended to leave 
          # this value at the release version of the first install of this system. 
          # Before changing this value read the documentation for this option 
          # (e.g. man configuration.nix or on https://nixos.org/nixos/options.html). 
       
     
   
  
    
    
  
  
 
The function itself works, using it like this works:
But I want to have all functions in ./utils available everywhere without importing it every time
             
            
               
               
               
            
           
          
            
              
                NobbZ  
                
               
              
                  
                    July 27, 2021,  2:02pm
                   
                   
              8 
               
             
            
              Where are you loading/importing/applying the overlay?
             
            
               
               
               
            
           
          
            
              
                pinpox  
                
               
              
                  
                    July 27, 2021,  3:41pm
                   
                   
              9 
               
             
            
              Here:
I found a workaround that works, but would prefer to have this in an overlay still if possible, so I can use it everywhere https://github.com/pinpox/nixos/blob/301ada9b0ec3d2ba7d4b28047a2269f1306ed567/home-manager/home.nix#L39 
             
            
               
               
               
            
           
          
            
              
                NobbZ  
                
               
              
                  
                    July 27, 2021,  3:45pm
                   
                   
              10 
               
             
            
              In the commit matching that flake.nix the lib doesn’t exist at all in the overlay… Or am I misreading your config?
             
            
               
               
               
            
           
          
            
              
                pinpox  
                
               
              
                  
                    July 27, 2021,  3:48pm
                   
                   
              11 
               
             
            
            
               
               
               
            
           
          
            
              
                NobbZ  
                
               
              
                  
                    July 27, 2021,  5:01pm
                   
                   
              12 
               
             
            
              The way you do it makes pinpox a function taking an attrset with at least the pkgs attribute.
PS: in flakes I’d just provide a lib-output, specific to self.