Skip to content
This repository has been archived by the owner on Mar 24, 2024. It is now read-only.

feat: use flakehub for installs, self-manage fleek, autogc #282

Merged
merged 6 commits into from
Sep 20, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions internal/flake/flake.go
Original file line number Diff line number Diff line change
Expand Up @@ -537,6 +537,21 @@ func (f *Flake) writeUser(sys fleek.System, user fleek.User, template string, fo

return nil
}

func (f *Flake) WriteTemplates() error {

writeCmdLine := []string{"run", ".#fleek", "--", "write"}
err := f.runNix(nixbin, writeCmdLine)
if err != nil {
return err
}
err = f.mayCommit("fleek: update templates")

if err != nil {
return err
}
return nil
}
func (f *Flake) Apply() error {
fin.Info.Println(f.app.Trans("flake.apply"))

Expand Down
13 changes: 10 additions & 3 deletions internal/flake/templates/flake.nix.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
nixpkgs.url = "github:nixos/nixpkgs/{{ .Config.Tracks }}";

# Home manager
home-manager.url = "github:nix-community/home-manager";
home-manager.url = "https://flakehub.com/f/nix-community/home-manager/0.1.tar.gz";
home-manager.inputs.nixpkgs.follows = "nixpkgs";

# Fleek
fleek.url = "github:ublue-os/fleek";
fleek.url = "https://flakehub.com/f/ublue-os/fleek/*.tar.gz";

# Overlays
{{ range $index, $element := .Config.Overlays }}
Expand All @@ -22,7 +22,9 @@
};

outputs = { self, nixpkgs, home-manager, fleek, ... }@inputs: {

{{ range .Config.Systems }}
packages.{{ .Arch }}-{{ .OS }}.fleek = fleek.packages.{{ .Arch }}-{{ .OS }}.default;
{{ end }}
# Available through 'home-manager --flake .#your-username@your-hostname'
{{ $overlays := .Config.Overlays }}
homeConfigurations = {
Expand All @@ -41,6 +43,11 @@
./{{.Hostname}}/{{.Username}}.nix
./{{.Hostname}}/custom.nix
# self-manage fleek
{
home.packages = [
fleek.packages.{{ .Arch }}-{{ .OS }}.default
];
}
({
nixpkgs.overlays = [{{ range $index, $element := $overlays }}inputs.{{$index}}.overlay {{ end }}];
})
Expand Down
15 changes: 15 additions & 0 deletions internal/fleek/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ type Config struct {
Users []*User `yaml:",flow"`
Track string `yaml:"track"`
AllowBroken bool `yaml:"allow_broken"`
AutoGC bool `yaml:"auto_gc"`
}

func Levels() []string {
Expand Down Expand Up @@ -120,6 +121,19 @@ func NewSystem() (*System, error) {
Username: user,
}, nil
}

// CollectGarbage runs nix-collect-garbage
func CollectGarbage() error {

command := exec.Command("nix-collect-garbage", "-d")
command.Stdin = os.Stdin
command.Stderr = os.Stderr
command.Stdout = os.Stdout
command.Env = os.Environ()

return command.Run()

}
func NewUser() (*User, error) {
fin.Info.Println("Enter User Details for Git Configuration:")
user := &User{}
Expand Down Expand Up @@ -460,6 +474,7 @@ func (c *Config) WriteInitialConfig(force bool, symlink bool) error {
return err
}
c.Unfree = true
c.AutoGC = true
c.Name = "Fleek Configuration"
c.Packages = []string{
"helix",
Expand Down
11 changes: 10 additions & 1 deletion internal/fleekcli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,13 @@ func RootCmd() *cobra.Command {
fin.Debug.Printfln("git autopush: %v", cfg.Git.AutoPush)
fin.Debug.Printfln("git autocommit: %v", cfg.Git.AutoCommit)
fin.Debug.Printfln("git autopull: %v", cfg.Git.AutoPull)
fin.Debug.Printfln("auto gc: %v", cfg.AutoGC)

if cfg.AutoGC {
fin.Info.Println("Running nix-collect-garbage")
// we don't care too much if there's an error here
_ = fleek.CollectGarbage()
}

},
RunE: func(cmd *cobra.Command, args []string) error {
Expand Down Expand Up @@ -152,6 +159,8 @@ func RootCmd() *cobra.Command {

infoCmd := InfoCommand()
infoCmd.GroupID = packageGroup.ID
writeCmd := WriteCommand()
writeCmd.GroupID = fleekGroup.ID
manCmd := ManCommand()

docsCmd := genDocsCmd()
Expand All @@ -171,7 +180,7 @@ func RootCmd() *cobra.Command {
command.AddCommand(searchCmd)
command.AddCommand(infoCmd)
command.AddCommand(generateCmd)

command.AddCommand(writeCmd)
command.AddCommand(VersionCmd())

command.PersistentFlags().BoolVarP(
Expand Down
6 changes: 6 additions & 0 deletions internal/fleekcli/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ func update(cmd *cobra.Command) error {
if err := fl.Update(); err != nil {
return err
}
// We just updated the flake lock, which might pull a new
// version of fleek in. Update the system templates to
// get new fixes without having to update/apply twice
if err := fl.WriteTemplates(); err != nil {
return err
}
if cmd.Flag(app.Trans("update.applyFlag")).Changed {
if err := fl.Apply(); err != nil {
return err
Expand Down
14 changes: 0 additions & 14 deletions internal/fleekcli/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (

"github.com/spf13/cobra"
"github.com/ublue-os/fleek/internal/build"
"github.com/ublue-os/fleek/internal/vercheck"
)

type versionFlags struct {
Expand All @@ -27,7 +26,6 @@ func VersionCmd() *cobra.Command {
command.Flags().BoolVarP(&flags.verbose, app.Trans("version.flagVerbose"), "v", false, // value
app.Trans("version.flagVerboseDescription"),
)
command.AddCommand(selfUpdateCmd())

return command
}
Expand All @@ -47,18 +45,6 @@ func versionCmdFunc(cmd *cobra.Command, _ []string, flags versionFlags) error {
}
return nil
}
func selfUpdateCmd() *cobra.Command {
command := &cobra.Command{
Use: "update",
Short: "Update fleek launcher and binary",
Args: cobra.ExactArgs(0),
RunE: func(cmd *cobra.Command, args []string) error {
return vercheck.SelfUpdate(cmd.OutOrStdout(), cmd.OutOrStderr())
},
}

return command
}

type versionInfo struct {
Version string
Expand Down
48 changes: 48 additions & 0 deletions internal/fleekcli/write.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
Copyright © 2023 NAME HERE <EMAIL ADDRESS>
*/
package fleekcli

import (
"github.com/spf13/cobra"
"github.com/ublue-os/fleek/fin"
"github.com/ublue-os/fleek/internal/flake"
)

// WriteCommand is an internal hidden command that
// gets run after an update.
func WriteCommand() *cobra.Command {
command := &cobra.Command{
Hidden: true,
Use: app.Trans("write.use"),
Short: app.Trans("write.short"),
Long: app.Trans("write.long"),
RunE: func(cmd *cobra.Command, args []string) error {
return write(cmd)
},
}

return command
}

// writeCmd represents the write command
func write(cmd *cobra.Command) error {
fin.Description.Println(cmd.Short)
err := mustConfig()
if err != nil {
return err
}
fl, err := flake.Load(cfg, app)
if err != nil {
return err
}

err = fl.Write("flake update", true, false)
if err != nil {
fin.Debug.Printfln("flake write error: %s", err)
return err
}

fin.Success.Println(app.Trans("write.done"))
return nil
}
7 changes: 7 additions & 0 deletions locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,13 @@ info:
notFound: "That program or package is not part of fleek's bling set."
aliases: "Shell Aliases"
description: "Description"
write:
use: "write"
long: "Apply system templates to existing flake"
example: |
fleek write
short: "Apply system templates to existing flake"
done: "Flake templates written."
flake:
noConfig: "No configuration files found. Try `fleek init`."
configLoaded: "Configuration loaded"
Expand Down