Skip to content

Commit

Permalink
Merge pull request #88 from snowfallorg/dev
Browse files Browse the repository at this point in the history
Snowfall Lib v3
  • Loading branch information
jakehamilton authored May 23, 2024
2 parents 3630355 + b2e6364 commit c580238
Show file tree
Hide file tree
Showing 23 changed files with 1,311 additions and 1,133 deletions.
20 changes: 12 additions & 8 deletions default.nix
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
(import
(
import
(
let lock = builtins.fromJSON (builtins.readFile ./flake.lock); in
fetchTarball {
url = "https://github.com/edolstra/flake-compat/archive/${lock.nodes.flake-compat.locked.rev}.tar.gz";
sha256 = lock.nodes.flake-compat.locked.narHash;
}
let
lock = builtins.fromJSON (builtins.readFile ./flake.lock);
in
fetchTarball {
url = "https://github.com/edolstra/flake-compat/archive/${lock.nodes.flake-compat.locked.rev}.tar.gz";
sha256 = lock.nodes.flake-compat.locked.narHash;
}
)
{ src = ./.; }
).defaultNix
{src = ./.;}
)
.defaultNix
8 changes: 4 additions & 4 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

121 changes: 66 additions & 55 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
description = "Snowfall Lib";

inputs = {
nixpkgs.url = "github:nixos/nixpkgs/release-22.11";
nixpkgs.url = "github:nixos/nixpkgs/release-23.11";
flake-utils-plus.url = "github:gytis-ivaskevicius/flake-utils-plus";

flake-compat = {
Expand All @@ -11,75 +11,86 @@
};
};

outputs = inputs:
let
core-inputs = inputs // {
outputs = inputs: let
core-inputs =
inputs
// {
src = ./.;
};

# Create the library, extending the nixpkgs library and merging
# libraries from other inputs to make them available like
# `lib.flake-utils-plus.mkApp`.
# Usage: mkLib { inherit inputs; src = ./.; }
# result: lib
mkLib = import ./snowfall-lib core-inputs;
# Create the library, extending the nixpkgs library and merging
# libraries from other inputs to make them available like
# `lib.flake-utils-plus.mkApp`.
# Usage: mkLib { inherit inputs; src = ./.; }
# result: lib
mkLib = import ./snowfall-lib core-inputs;

# A convenience wrapper to create the library and then call `lib.mkFlake`.
# Usage: mkFlake { inherit inputs; src = ./.; ... }
# result: <flake-outputs>
mkFlake = flake-and-lib-options@{ inputs, src, snowfall ? { }, ... }:
let
lib = mkLib {
inherit inputs src snowfall;
};
flake-options = builtins.removeAttrs flake-and-lib-options [ "inputs" "src" ];
in
lib.mkFlake flake-options;
# A convenience wrapper to create the library and then call `lib.mkFlake`.
# Usage: mkFlake { inherit inputs; src = ./.; ... }
# result: <flake-outputs>
mkFlake = flake-and-lib-options @ {
inputs,
src,
snowfall ? {},
...
}: let
lib = mkLib {
inherit inputs src snowfall;
};
flake-options = builtins.removeAttrs flake-and-lib-options ["inputs" "src"];
in
{
inherit mkLib mkFlake;
lib.mkFlake flake-options;
in {
inherit mkLib mkFlake;

nixosModules = {
user = ./modules/nixos/user/default.nix;
};
nixosModules = {
user = ./modules/nixos/user/default.nix;
};

darwinModules = {
user = ./modules/darwin/user/default.nix;
};
darwinModules = {
user = ./modules/darwin/user/default.nix;
};

homeModules = {
user = ./modules/home/user/default.nix;
};
homeModules = {
user = ./modules/home/user/default.nix;
};

_snowfall = rec {
formatter = {
x86_64-linux = inputs.nixpkgs.legacyPackages.x86_64-linux.alejandra;
aarch64-linux = inputs.nixpkgs.legacyPackages.aarch64-linux.alejandra;
x86_64-darwin = inputs.nixpkgs.legacyPackages.x86_64-darwin.alejandra;
aarch64-darwin = inputs.nixpkgs.legacyPackages.aarch64-darwin.alejandra;
};

raw-config = config;
snowfall = rec {
raw-config = config;

config = {
root = ./.;
src = ./.;
namespace = "snowfall";
lib-dir = "snowfall-lib";
config = {
root = ./.;
src = ./.;
namespace = "snowfall";
lib-dir = "snowfall-lib";

meta = {
name = "snowfall-lib";
title = "Snowfall Lib";
};
meta = {
name = "snowfall-lib";
title = "Snowfall Lib";
};
};

internal-lib =
let
lib = mkLib {
src = ./.;
internal-lib = let
lib = mkLib {
src = ./.;

inputs = inputs // {
self = { };
};
inputs =
inputs
// {
self = {};
};
in
builtins.removeAttrs
lib.snowfall
[ "internal" ];
};
};
in
builtins.removeAttrs
lib.snowfall
["internal"];
};
};
}
94 changes: 59 additions & 35 deletions modules/darwin/user/default.nix
Original file line number Diff line number Diff line change
@@ -1,29 +1,45 @@
{ pkgs, lib, options, config, inputs, ... }:

let
inherit (lib) types mkOption mkDefault foldl optionalAttrs;
{
pkgs,
lib,
options,
config,
inputs,
...
}: let
inherit
(lib)
types
mkOption
mkDefault
mkRenamedOptionModule
foldl
optionalAttrs
;

cfg = config.snowfallorg;

user-names = builtins.attrNames cfg.user;
user-names = builtins.attrNames cfg.users;

create-system-users = system-users: name:
let
user = cfg.user.${name};
in
system-users // (optionalAttrs user.create {
create-system-users = system-users: name: let
user = cfg.users.${name};
in
system-users
// (optionalAttrs user.create {
${name} = {
home = mkDefault user.home.path;
isHidden = mkDefault false;
};
});
in
{
in {
imports = [
(mkRenamedOptionModule ["snowfallorg" "user"] ["snowfallorg" "users"])
];

options.snowfallorg = {
user = mkOption {
users = mkOption {
description = "User configuration.";
default = { };
type = types.attrsOf (types.submodule ({ name, ... }: {
default = {};
type = types.attrsOf (types.submodule ({name, ...}: {
options = {
create = mkOption {
description = "Whether to create the user automatically.";
Expand All @@ -46,28 +62,36 @@ in
# HM-compatible options taken from:
# https://github.com/nix-community/home-manager/blob/0ee5ab611dc1fbb5180bd7d88d2aeb7841a4d179/nixos/common.nix#L14
type = types.submoduleWith {
specialArgs = {
osConfig = config;
modulesPath = "${inputs.home-manager}/modules";
} // config.home-manager.extraSpecialArgs;
modules = [
({ lib, modulesPath, ... }: {
imports = import "${modulesPath}/modules.nix" {
inherit pkgs lib;
useNixpkgsModule = !config.home-manager.useGlobalPkgs;
};
specialArgs =
{
osConfig = config;
modulesPath = "${inputs.home-manager}/modules";
}
// config.home-manager.extraSpecialArgs;
modules =
[
({
lib,
modulesPath,
...
}: {
imports = import "${modulesPath}/modules.nix" {
inherit pkgs lib;
useNixpkgsModule = !config.home-manager.useGlobalPkgs;
};

config = {
submoduleSupport.enable = true;
submoduleSupport.externalPackageInstall = cfg.useUserPackages;
config = {
submoduleSupport.enable = true;
submoduleSupport.externalPackageInstall = cfg.useUserPackages;

home.username = config.users.users.${name}.name;
home.homeDirectory = config.users.users.${name}.home;
home.username = config.users.users.${name}.name;
home.homeDirectory = config.users.users.${name}.home;

nix.package = config.nix.package;
};
})
] ++ config.home-manager.sharedModules;
nix.package = config.nix.package;
};
})
]
++ config.home-manager.sharedModules;
};
};
};
Expand All @@ -77,6 +101,6 @@ in
};

config = {
users.users = (foldl create-system-users { } (user-names));
users.users = foldl create-system-users {} user-names;
};
}
26 changes: 14 additions & 12 deletions modules/home/user/default.nix
Original file line number Diff line number Diff line change
@@ -1,25 +1,27 @@
inputs@{ pkgs, lib, options, config, ... }:

let
inputs @ {
pkgs,
lib,
options,
config,
...
}: let
inherit (lib) types mkOption mkIf mkDefault;

cfg = config.snowfallorg;

# @NOTE(jakehamilton): The module system chokes if it finds `osConfig` named in the module arguments
# NOTE: The module system chokes if it finds `osConfig` named in the module arguments
# when being used in standalone home-manager. To remedy this, we have to refer to the arguments set directly.
os-user-home = inputs.osConfig.users.users.${cfg.name}.home or null;

has-user-name = (cfg.user.name or null) != null;

default-home-directory =
if (os-user-home != null) then
os-user-home
else if pkgs.stdenv.isDarwin then
"/Users/${cfg.user.name}"
else
"/home/${cfg.user.name}";
in
{
if (os-user-home != null)
then os-user-home
else if pkgs.stdenv.isDarwin
then "/Users/${cfg.user.name}"
else "/home/${cfg.user.name}";
in {
options.snowfallorg = {
user = {
enable = mkOption {
Expand Down
Loading

0 comments on commit c580238

Please sign in to comment.