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

Regarding nixpkgs.lib.nixosSystem.specialArgs #7

Closed
shadowrylander opened this issue Oct 17, 2020 · 5 comments
Closed

Regarding nixpkgs.lib.nixosSystem.specialArgs #7

shadowrylander opened this issue Oct 17, 2020 · 5 comments
Labels

Comments

@shadowrylander
Copy link

Hello!

I was just wondering whether you could tell me whether the specialArgs set can pass a custom lib to the configuration files? I'd like to make a custom function automatically available to all my [sub-]modules directly, and I was wondering whether this could do the trick!

Thank you kindly for the help!

@hlissner
Copy link
Owner

hlissner commented Oct 17, 2020

Yes, it can, and is in fact exactly what I'm doing. Before that, I merged the built-in lib and mine.

Now my personal library is available everywhere under lib.my.

@shadowrylander
Copy link
Author

Perfect! Mind if I copy your lib setup, then? 😺

@hlissner
Copy link
Owner

Feel free. Glad I could help!

@shadowrylander
Copy link
Author

shadowrylander commented Oct 18, 2020

Thanks! However, could you help me with this? I'm getting the error attribute '_' missing in my config, which is what I called my extra modules instead of my; lib/default.nix imports just fine, though.
configuration.nix:

# Edit this configuration file to define what should be installed on
# your system.  Help is available in the configuration.nix(5) man page
# and in the NixOS manual (accessible by running ‘nixos-help’).

{ config, pkgs, lib, ... }:

# let extraBuiltins = (import /etc/nixos/shared/global/_extraBuiltins.nix { inherit lib; }); in {
{
    imports = lib._.zettelkasten.list [];

    config = lib.mkMerge [{

            powerManagement = {
                enable = true;
                cpuFreqGovernor = lib.mkDefault "powersave";
            };

            # Select internationalisation properties.
            i18n.defaultLocale = "en_US.UTF-8";
            console = {
                # font = lib.mkDefault "${pkgs.terminus_font}/share/consolefonts/ter-u28n.psf.gz";
                font = "Cartograph CF Light Italic";
                keyMap = "us";
            };

            # Set your time zone.
            time.timeZone = "America/Toronto";

            # Enable sound.
            sound.enable = true;
            hardware.pulseaudio.enable = true;

            # 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).
        }
    ];
}

flake.nix:

{
  description = "shadowrylander";

  inputs = rec {
    home-manager = {
      url = "github:nix-community/home-manager/master";
      # https://github.com/nix-community/home-manager/blob/master/flake.nix#L4
      # HM takes 'nixpkgs' as input
      inputs.nixpkgs.follows = "nixpkgs";
    };
    mach-nix = {
        # url = "github:davhau/mach-nix/master";
        # url = "/home/shadowrylander/mach-nix";
        url = "/etc/nixos/extras/mach-nix";
        inputs.nixpkgs.follows = "nixpkgs";
    };
    impermanence = {
      url = "github:nix-community/impermanence";
      flake = false;
    };
    flake-utils = {
        url = "github:numtide/flake-utils";
        inputs.nixpkgs.follows = "nixpkgs";
    };
    nur = {
      url = github:nix-community/NUR;
      inputs.nixpkgs.follows = "nixpkgs";
    };

    r2003.url = "github:NixOS/nixpkgs/nixos-20.03";
    r2003small.url = "github:NixOS/nixpkgs/nixos-20.03-small";
    r2009.url = "github:NixOS/nixpkgs/nixos-20.09";
    r2009small.url = "github:NixOS/nixpkgs/nixos-20.09-small";
    # r2103.url = "github:NixOS/nixpkgs/nixos-21.03";
    # r2103small.url = "github:NixOS/nixpkgs/nixos-21.03-small";
    unstable.url = "github:NixOS/nixpkgs/nixos-unstable";
    small.url = "github:NixOS/nixpkgs/nixos-unstable-small";
    master.url = "github:NixOS/nixpkgs/master";

    nixpkgs.follows = "master";

    nix = { url = "github:NixOS/nix/master"; };

  };

  outputs = inputs@{ self, nixpkgs, flake-utils, ... }: let

    createOverlay = name: {
      inherit name;
      # value = final: prev: { "${name}" = inputs.${name}.legacyPackages.${system}; };
      value = final: prev: { "${name}" = inputs.${name}; };
    };

    global = "/shared/global";

    _pkgs = {
      set = (import nixpkgs {});
      func = _: (import nixpkgs _);
    };
    
    ########################################################
    lib = (import (./. + "${global}/lib") { inherit nixpkgs; });
    inherit (lib) nameValuePair mapAttrsToList removeSuffix filterAttrs hasSuffix;
    ########################################################

  in rec {

    overlays = (map createOverlay [
      "r2003"
      "r2003small"
      "r2009"
      "r2009small"
      # "r2103"
      # "r2103small"
      "unstable"
      "small"
    ]) ++ [ inputs.nur.overlay ];

    nixosConfigurations = let

      ##########################
      specialArgs = { inherit lib inputs; };
      ##########################

      configDir = "/configs";

      create = hostname: let

        system = if (
          hostname == "bastion"
        ) then "armv7l-linux" else "x86_64-linux";

        # system = lib.mkIf lib.mkForce ( hostname == "flipper" ) "";

        config = ./. + "${configDir}/${hostname}.nix";

        pkgs = _pkgs.func (import (./. + "${global}/_nixpkgs.nix") {
          inherit lib;
          # mach-nix = inputs.mach-nix.packages.${system}.mach-nix;
          stdenv = {
            inherit system;
            package = _pkgs.set.gcc10.stdenv;
          };
          pkgs = _pkgs.set;
        });

        base = {
          global = {
            modules = with inputs; [
              home-manager.nixosModules.home-manager
              "${impermanence}/nixos.nix"
              # "${impermanence}/home-manager.nix"
            ];
          };
        };

      in nixpkgs.lib.nixosSystem {
        inherit system pkgs;
        modules = base.global.modules ++ [ (import config) ];
      };

      devices = mapAttrsToList (
        name: value: removeSuffix ".nix" name
      ) (
        filterAttrs (
          name: value: (value == "regular") && (hasSuffix ".nix" name)
        ) (builtins.readDir (./. + configDir))
      );

    in (builtins.listToAttrs (map (hostname: nameValuePair hostname (create hostname)) devices)) // {
      # [...]
    };
  };
}

lib/default.nix:

{ nixpkgs, ... }:

# From: https://github.com/hlissner/dotfiles

nixpkgs.lib.extend (self: super: { _ = let
        inherit (self) mapAttrsToList filterAttrs hasPrefix any removeSuffix getName nameValuePair;
        inherit (self) makeExtensible attrValues foldr;

        zettelkasten = rec {
            list = ignores: let
                _ignores = ignores ++ [ "default" ];
            in mapAttrsToList (
                name: value: ./. + "/${name}"
            ) (
                filterAttrs ( name: value:
                    (value == "regular") &&
                    (!hasPrefix "_" name) &&
                    (!any (ign: (removeSuffix ".nix" name) == ign) _ignores)
                ) (builtins.readDir ./.)
            );
            set = ignores: builtins.listToAttrs (
                map (file: nameValuePair (removeSuffix ".nix" (builtins.unsafeDiscardStringContext file)) (import file { inherit nixpkgs; })
            ) (list ignores));
        };

        __ = makeExtensible (self: { inherit zettelkasten; } // zettelkasten.set []);
    in __.extend(self: super: foldr (new: old: new // old) {} (attrValues super));
})

@shadowrylander
Copy link
Author

Nope, never mind; I put the specialArgs in the let statement, without actually using it. My bad! 😅😹

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants