Skip to content

Commit

Permalink
Did some cleanup, simplification, and added more git options, also ad…
Browse files Browse the repository at this point in the history
…ded the abbilities for plugins to be built as derivations"

added testing for coerceGit

Revert 'package update'

added the abbility to specify a local git repo successfully and without error, note: will need to clean up comments after testing

Added userplugin custom derviation support

Making sure userplugins is evaluated

Undoing inherit

ensure that a local path works

added vencordPkgs as a input

added debugging

Re-added testing on new branch

Changed defaultVencord to vencordPkgs (as I changed it in my testing)

Fixed rebase mishap

Removed some traces
  • Loading branch information
SpiderUnderUrBed committed Jan 24, 2025
1 parent 9d5dc01 commit 717c9fb
Show file tree
Hide file tree
Showing 2 changed files with 204 additions and 143 deletions.
345 changes: 203 additions & 142 deletions hm-module.nix
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
}:
let
cfg = config.programs.nixcord;

inherit (lib)
mkEnableOption
mkOption
Expand All @@ -16,53 +15,135 @@ let
attrsets
lists
;

vencordPkgs = pkgs.callPackage ./vencord.nix {
inherit (pkgs)
curl
esbuild
fetchFromGitHub
git
jq
lib
nix-update
nodejs
pnpm
stdenv
writeShellScript
;
buildWebExtension = false;
};

applyPostPatch = pkg: pkg.overrideAttrs (oldAttrs: {
postPatch = ''
ln -s ${userPluginsDirectory} src/userplugins
'';
});

patchedVencord = (applyPostPatch vencordPkgs);

dop = with types; coercedTo package (a: a.outPath) pathInStore;

recursiveUpdateAttrsList =
list:
if (builtins.length list <= 1) then
(builtins.elemAt list 0)
# Define regular expressions for GitHub and Git URLs
regexGithub = "github:([[:alnum:].-]+)/([[:alnum:]/-]+)/([0-9a-f]{40})";
regexGit = "git[+]file:///([^/]+/)*([^/?]+)(\\?ref=[a-f0-9]{40})?$";

# Define coercion functions for GitHub and Git
coerceGithub = value: let
matches = builtins.match regexGithub value;
owner = builtins.elemAt matches 0;
repo = builtins.elemAt matches 1;
rev = builtins.elemAt matches 2;
in builtins.fetchGit {
url = "https://github.com/${owner}/${repo}";
inherit rev;
};

coerceGit = value: let
# Match using regex, assuming regexGit is defined and captures groups correctly
matches = builtins.match regexGit value;

# Set rev only if matches are found
rev = if matches != null then
let
rawRev = builtins.elemAt matches 2;
in
if rawRev != null && builtins.substring 0 5 rawRev == "?ref="
then builtins.substring 5 (builtins.stringLength rawRev) rawRev
else null
else null;

# Set filepath only if matches are found
filepath = if matches != null then
let
startOffset = 4; # Remove 4 characters from the beginning
endOffset = 45; # Remove 45 characters from the end
fullLength = builtins.stringLength value;
adjustedPathLength = fullLength - startOffset - endOffset;
in
builtins.substring startOffset adjustedPathLength value
else null;

in if filepath != null then
# Call fetchGit only if filepath is valid
builtins.fetchGit (
let
# Only include rev if it's non-null and non-empty
revCondition = if rev != null && rev != "" then { rev = rev; } else {};
in {
url = filepath;
ref = "main";
} // revCondition
)
else
throw "Failed to extract a valid filepath from the given value";

# Mapper function that applies coercion based on the regex match
pluginMapper = plugin:
if builtins.match regexGithub plugin != null then
coerceGithub plugin
else if builtins.match regexGit plugin != null then
coerceGit plugin
else if lib.attrsets.isDerivation plugin then
plugin
else
recursiveUpdateAttrsList (
[
(attrsets.recursiveUpdate (builtins.elemAt list 0) (builtins.elemAt list 1))
]
++ (lists.drop 2 list)
);
builtins.path {
name = "plugin";
path = builtins.toPath plugin;
};

applyPostPatch =
pkg:
pkg.overrideAttrs {
postPatch = lib.concatLines (
lib.optional (cfg.userPlugins != { }) "mkdir -p src/userplugins"
++ lib.mapAttrsToList (
name: path:
"ln -s ${lib.escapeShellArg path} src/userplugins/${lib.escapeShellArg name} && ls src/userplugins"
) cfg.userPlugins
);
};
recursiveUpdateAttrsList = list:
if (builtins.length list <= 1) then (builtins.elemAt list 0) else
recursiveUpdateAttrsList ([
(attrsets.recursiveUpdate (builtins.elemAt list 0) (builtins.elemAt list 1))
] ++ (lists.drop 2 list));

defaultVencord = applyPostPatch (
pkgs.callPackage ./vencord.nix {
inherit (pkgs)
curl
esbuild
fetchFromGitHub
git
jq
lib
nix-update
nodejs
pnpm
stdenv
writeShellScript
;
buildWebExtension = false;
}
);
in
pluginDerivations = lib.mapAttrs (_: plugin: pluginMapper plugin) cfg.userPlugins;
#apiPath = vencordPkgs.outDir;
apiPath = vencordPkgs.src;
buildDirs = pluginDerivations: lib.mapAttrsToList (name: pluginDir:
let
fullPath = "${pluginDir}";

# Check for a Nix expression and build if present
buildIfExists = if builtins.pathExists "${fullPath}/default.nix" || builtins.pathExists "${fullPath}/shell.nix" then
import fullPath {
inherit pkgs
apiPath;
}

else
pluginDir;
in
# Return an attribute set with a `name` and `path` for linkFarm
{ name = name; path = buildIfExists; }
) pluginDerivations;
# Build the user plugins directory with linkFarm
userPluginsDirectory = pkgs.linkFarm "userPlugins" (buildDirs pluginDerivations);

in
{

#inherit patchedVencord;
options.programs.nixcord = {
enable = mkEnableOption "Enables Discord with Vencord";
discord = {
Expand Down Expand Up @@ -99,7 +180,7 @@ in
};
package = mkOption {
type = types.package;
default = defaultVencord;
default = vencordPkgs;
description = ''
The Vencord package to use
'';
Expand Down Expand Up @@ -240,30 +321,24 @@ in
for both vencord and vesktop
'';
};
userPlugins =
let
regex = "github:([[:alnum:].-]+)/([[:alnum:]/-]+)/([0-9a-f]{40})";
coerce =
value:
let
matches = builtins.match regex value;
owner = builtins.elemAt matches 0;
repo = builtins.elemAt matches 1;
rev = builtins.elemAt matches 2;
in
builtins.fetchGit {
url = "https://github.com/${owner}/${repo}";
inherit rev;
};
in
mkOption {
type = with types; attrsOf (coercedTo (strMatching regex) coerce dop);
description = "User plugin to fetch and install. Note that any json required must be enabled in extraConfig";
default = { };
example = {
someCoolPlugin = "github:someUser/someCoolPlugin/someHashHere";
};
userPlugins = lib.mkOption {
description = "User plugin to fetch and install. Note that any JSON required must be enabled in extraConfig.";

# Define the type of userPlugins with validation
type = types.attrsOf (types.oneOf [
(types.strMatching regexGithub) # GitHub URL pattern
(types.strMatching regexGit) # Git URL pattern
types.package # Nix packages
types.path # Nix paths
]);

# Example usage of the userPlugins option
example = {
someCoolPlugin = "github:someUser/someCoolPlugin/someHashHere"; # GitHub example
anotherCoolPlugin = "git+file:///path/to/repo?rev=abcdef1234567890abcdef1234567890abcdef12"; # File path example
};
};

parseRules = {
upperNames = mkOption {
type = with types; listOf str;
Expand Down Expand Up @@ -307,85 +382,71 @@ in
};
};

config =
let
parseRules = cfg.parseRules;
inherit (pkgs.callPackage ./lib.nix { inherit lib parseRules; })
mkVencordCfg
;
config = let
parseRules = cfg.parseRules;
inherit (pkgs.callPackage ./lib.nix { inherit lib parseRules; })
mkVencordCfg;
vencord = patchedVencord;

vencord = applyPostPatch cfg.discord.vencord.package;

isQuickCssUsed =
appConfig:
(cfg.config.useQuickCss || appConfig ? "useQuickCss" && appConfig.useQuickCss)
&& cfg.quickCss != "";
in
mkIf cfg.enable (mkMerge [
isQuickCssUsed = appConfig: (cfg.config.useQuickCss || appConfig ? "useQuickCss" && appConfig.useQuickCss) && cfg.quickCss != "";
in mkIf cfg.enable (mkMerge [
{
home.packages = [
(mkIf cfg.discord.enable (cfg.discord.package.override {
withVencord = cfg.discord.vencord.enable;
withOpenASAR = cfg.discord.openASAR.enable;
inherit vencord;
}))
(mkIf cfg.vesktop.enable (cfg.vesktop.package.override {
withSystemVencord = true;
inherit vencord;
}))
];
}
(mkIf cfg.discord.enable (mkMerge [
# QuickCSS
(mkIf (isQuickCssUsed cfg.vencordConfig) {
home.file."${cfg.configDir}/settings/quickCss.css".text = cfg.quickCss;
})
# Vencord Settings
{
home.packages = [
(mkIf cfg.discord.enable (
cfg.discord.package.override {
withVencord = cfg.discord.vencord.enable;
withOpenASAR = cfg.discord.openASAR.enable;
inherit vencord;
}
))
(mkIf cfg.vesktop.enable (
cfg.vesktop.package.override {
withSystemVencord = true;
inherit vencord;
}
))
];
home.file."${cfg.configDir}/settings/settings.json".text =
builtins.toJSON (mkVencordCfg (
recursiveUpdateAttrsList [ cfg.config cfg.extraConfig cfg.vencordConfig ]
));
}
(mkIf cfg.discord.enable (mkMerge [
# QuickCSS
(mkIf (isQuickCssUsed cfg.vencordConfig) {
home.file."${cfg.configDir}/settings/quickCss.css".text = cfg.quickCss;
})
# Vencord Settings
{
home.file."${cfg.configDir}/settings/settings.json".text = builtins.toJSON (
mkVencordCfg (recursiveUpdateAttrsList [
cfg.config
cfg.extraConfig
cfg.vencordConfig
])
);
}
# Client Settings
(mkIf (cfg.discord.settings != { }) {
home.file."${cfg.discord.configDir}/settings.json".text = builtins.toJSON mkVencordCfg cfg.discord.settings;
})
]))
(mkIf cfg.vesktop.enable (mkMerge [
# QuickCSS
(mkIf (isQuickCssUsed cfg.vesktopConfig) {
home.file."${cfg.vesktop.configDir}/settings/quickCss.css".text = cfg.quickCss;
})
# Vencord Settings
{
home.file."${cfg.vesktop.configDir}/settings/settings.json".text = builtins.toJSON (
mkVencordCfg (recursiveUpdateAttrsList [
cfg.config
cfg.extraConfig
cfg.vesktopConfig
])
);
}
# Vesktop Client Settings
(mkIf (cfg.vesktop.settings != { }) {
home.file."${cfg.vesktop.configDir}/settings.json".text = builtins.toJSON mkVencordCfg cfg.vesktopSettings;
})
# Vesktop Client State
(mkIf (cfg.vesktop.state != { }) {
home.file."${cfg.vesktop.configDir}/state.json".text = builtins.toJSON mkVencordCfg cfg.vesktopState;
})
]))
# Warnings
# Client Settings
(mkIf (cfg.discord.settings != {}) {
home.file."${cfg.discord.configDir}/settings.json".text =
builtins.toJSON mkVencordCfg cfg.discord.settings;
})
]))
(mkIf cfg.vesktop.enable (mkMerge [
# QuickCSS
(mkIf (isQuickCssUsed cfg.vesktopConfig) {
home.file."${cfg.vesktop.configDir}/settings/quickCss.css".text = cfg.quickCss;
})
# Vencord Settings
{
warnings = import ./warnings.nix { inherit cfg mkIf; };
home.file."${cfg.vesktop.configDir}/settings/settings.json".text =
builtins.toJSON (mkVencordCfg (
recursiveUpdateAttrsList [ cfg.config cfg.extraConfig cfg.vesktopConfig ]
));
}
]);
}
# Vesktop Client Settings
(mkIf (cfg.vesktop.settings != {}) {
home.file."${cfg.vesktop.configDir}/settings.json".text =
builtins.toJSON mkVencordCfg cfg.vesktopSettings;
})
# Vesktop Client State
(mkIf (cfg.vesktop.state != {}) {
home.file."${cfg.vesktop.configDir}/state.json".text =
builtins.toJSON mkVencordCfg cfg.vesktopState;
})
]))
# Warnings
{
warnings = import ./warnings.nix { inherit cfg mkIf; };
}
]);
}
2 changes: 1 addition & 1 deletion vencord.nix
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ stdenv.mkDerivation (finalAttrs: {
pnpmDeps = pnpm.fetchDeps {
inherit (finalAttrs) pname src;

hash = "sha256-vVzERis1W3QZB/i6SQR9dQR56yDWadKWvFr+nLTQY9Y=";
hash = "sha256-YBWe4MEmFu8cksOIxuTK0deO7q0QuqgOUc9WkUNBwp0=";
};

nativeBuildInputs = [
Expand Down

0 comments on commit 717c9fb

Please sign in to comment.