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

Commit

Permalink
feat: migrate users to system (#292)
Browse files Browse the repository at this point in the history
  • Loading branch information
bketelsen authored Sep 22, 2023
1 parent a281af6 commit b1d840e
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 25 deletions.
33 changes: 17 additions & 16 deletions internal/flake/flake.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,8 @@ func (f *Flake) Create(force bool, symlink bool) error {
if err != nil {
return err
}
user := f.Config.UserForSystem(sys.Hostname)
//user := f.Config.UserForSystem(sys.Hostname)
user := sys.User
data := Data{
Config: f.Config,
Bling: bling,
Expand Down Expand Up @@ -167,7 +168,7 @@ func (f *Flake) Create(force bool, symlink bool) error {
return err
}

err = f.writeSystem(*sys, "templates/host.nix.tmpl", force)
err = f.writeSystem(sys, "templates/host.nix.tmpl", force)
if err != nil {
return err
}
Expand Down Expand Up @@ -218,11 +219,8 @@ func (f *Flake) Join() error {
return err
}
csym := filepath.Join(home, ".fleek.yml")
err = os.Symlink(cfile, csym)
if err != nil {
fin.Debug.Println("first symlink attempt failed")
return err
}
// ignore if it exists, could have been created by caller
_ = os.Symlink(cfile, csym)

err = f.Config.Validate()
if err != nil {
Expand All @@ -246,7 +244,7 @@ func (f *Flake) Join() error {
}
if !found {
f.Config.Systems = append(f.Config.Systems, sys)
err = f.writeSystem(*sys, "templates/host.nix.tmpl", true)
err = f.writeSystem(sys, "templates/host.nix.tmpl", true)
if err != nil {
return err
}
Expand All @@ -257,8 +255,8 @@ func (f *Flake) Join() error {
return err
}
userFound := false
for _, u := range f.Config.Users {
if u.Username == username {
if sys.User != nil {
if sys.User.Username == username {
userFound = true
}
}
Expand All @@ -267,7 +265,8 @@ func (f *Flake) Join() error {
if err != nil {
return err
}
f.Config.Users = append(f.Config.Users, user)

sys.User = user
err = f.writeUser(*sys, *user, "templates/user.nix.tmpl", true)
if err != nil {
return err
Expand Down Expand Up @@ -383,14 +382,15 @@ func (f *Flake) Write(message string, writeHost, writeUser bool) error {
}
if writeHost {

err = f.writeSystem(*sys, "templates/host.nix.tmpl", force)
err = f.writeSystem(sys, "templates/host.nix.tmpl", force)
if err != nil {
return err
}
}
if writeUser {

user := f.Config.UserForSystem(sys.Hostname)
//user := f.Config.UserForSystem(sys.Hostname)
user := sys.User
err = f.writeUser(*sys, *user, "templates/user.nix.tmpl", true)
if err != nil {
return err
Expand Down Expand Up @@ -462,7 +462,7 @@ func (f *Flake) writeFile(template string, path string, d Data, force bool) erro
return nil
}

func (f *Flake) writeSystem(sys fleek.System, template string, force bool) error {
func (f *Flake) writeSystem(sys *fleek.System, template string, force bool) error {
var user *fleek.User
var err error
user = f.Config.UserForSystem(sys.Hostname)
Expand All @@ -471,14 +471,15 @@ func (f *Flake) writeSystem(sys fleek.System, template string, force bool) error
if err != nil {
return err
}
f.Config.Users = append(f.Config.Users, user)

sys.User = user
err = f.Config.Save()
if err != nil {
return err
}
}
sysData := SystemData{
System: sys,
System: *sys,
User: *user,
}
if f.Config.BYOGit {
Expand Down
4 changes: 2 additions & 2 deletions internal/flake/templates/flake.nix.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
{{ $overlays := .Config.Overlays }}
homeConfigurations = {
{{ range .Config.Systems }}
"{{ .Username }}@{{ .Hostname }}" = home-manager.lib.homeManagerConfiguration {
"{{ .User.Username }}@{{ .Hostname }}" = home-manager.lib.homeManagerConfiguration {
pkgs = nixpkgs.legacyPackages.{{ .Arch }}-{{ .OS }}; # Home-manager requires 'pkgs' instance
extraSpecialArgs = { inherit inputs; }; # Pass flake inputs to our config
modules = [
Expand All @@ -40,7 +40,7 @@
./aliases.nix
./programs.nix
# Host Specific configs
./{{.Hostname}}/{{.Username}}.nix
./{{.Hostname}}/{{.User.Username}}.nix
./{{.Hostname}}/custom.nix
# self-manage fleek
{
Expand Down
34 changes: 27 additions & 7 deletions internal/fleek/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ var (
shells = []string{"bash", "zsh"}
blingLevels = []string{"none", "low", "default", "high"}
LowPackages = []string{"htop", "git", "github-cli", "glab"}
DefaultPackages = []string{"fzf", "ripgrep", "vscode"}
DefaultPackages = []string{"fzf", "ripgrep", "vscode", "just"}
HighPackages = []string{"lazygit", "jq", "yq", "neovim", "neofetch", "btop", "cheat"}
LowPrograms = []string{"starship"}
DefaultPrograms = []string{"direnv"}
Expand Down Expand Up @@ -80,6 +80,7 @@ type System struct {
Arch string `yaml:"arch"`
OS string `yaml:"os"`
Home string `yaml:"home"`
User *User `yaml:"user"`
}

type User struct {
Expand Down Expand Up @@ -344,6 +345,10 @@ func (c *Config) UserForSystem(system string) *User {
userSystem = sys
}
}
if userSystem.User != nil {
return userSystem.User
}
// legacy unmigrated users
for _, u := range c.Users {
if u.Username == userSystem.Username {
return u
Expand Down Expand Up @@ -490,6 +495,12 @@ func (c *Config) WriteInitialConfig(force bool, symlink bool) error {
fin.Debug.Printfln("new system err: %s ", err)
return err
}
user, err := NewUser()
if err != nil {
fin.Debug.Printfln("new user err: %s ", err)
return err
}
sys.User = user
c.Unfree = true
c.AutoGC = true
c.Name = "Fleek Configuration"
Expand All @@ -512,12 +523,6 @@ func (c *Config) WriteInitialConfig(force bool, symlink bool) error {
c.Git.AutoCommit = true
c.Git.AutoPull = true
c.Git.AutoPush = true
user, err := NewUser()
if err != nil {
fin.Debug.Printfln("new user err: %s ", err)
return err
}
c.Users = []*User{user}

cfile, err := c.Location()
if err != nil {
Expand Down Expand Up @@ -643,6 +648,11 @@ func (c *Config) NeedsMigration() bool {

return true
}
if s.User == nil {
fin.Info.Println("Found unmigrated system users")
return true
}

}
return false
}
Expand Down Expand Up @@ -684,6 +694,16 @@ func (c *Config) Migrate() error {
return err
}
}
if s.User == nil {
fin.Info.Println("Migrating Users to System:", s.Hostname)
sysuser := c.UserForSystem(s.Hostname)

s.User = sysuser
err := c.Save()
if err != nil {
return err
}
}
}
return nil
}
44 changes: 44 additions & 0 deletions internal/fleekcli/join.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,50 @@ func join(cmd *cobra.Command, args []string) error {
if err != nil {
return err
}
// reload config and flake
config, err = fleek.ReadConfig(config.UserFlakeDir())
if err != nil {
return err
}
migrate := config.NeedsMigration()
if migrate {
fin.Info.Println("Migration required")
err := config.Migrate()
if err != nil {
fin.Error.Println("error migrating host files:", err)
os.Exit(1)
}
fl, err := flake.Load(config, app)
if err != nil {
fin.Error.Println("error loading flake:", err)
os.Exit(1)
}

// Symlink the yaml file to home
cfile, err := fl.Config.Location()
if err != nil {
fin.Debug.Printfln("location err: %s ", err)
return err
}
fin.Debug.Printfln("init cfile: %s ", cfile)

home, err := os.UserHomeDir()
if err != nil {
return err
}
csym := filepath.Join(home, ".fleek.yml")
err = os.Symlink(cfile, csym)
if err != nil {
fin.Debug.Println("symlink failed")
return err
}
err = fl.Write("update host and user files", true, false)
if err != nil {
fin.Error.Println("error writing flake:", err)
os.Exit(1)
}
}

fin.Info.Println(app.Trans("init.joining"))
fl, err := flake.Load(config, app)
if err != nil {
Expand Down

0 comments on commit b1d840e

Please sign in to comment.